package fr.titionfire.ffsaf.rest.data;

import fr.titionfire.ffsaf.data.model.CatPresetModel;
import fr.titionfire.ffsaf.data.model.CompetitionGuestModel;
import fr.titionfire.ffsaf.data.model.CompetitionModel;
import fr.titionfire.ffsaf.data.model.RegisterModel;
import fr.titionfire.ffsaf.utils.Categorie;
import fr.titionfire.ffsaf.utils.CompetitionSystem;
import fr.titionfire.ffsaf.utils.RegisterMode;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Data
@AllArgsConstructor
@RegisterForReflection
public class CompetitionData {
    private Long id;
    private String name;
    private String description;
    private String adresse;
    private String uuid;
    private Date date;
    private Date toDate;
    private CompetitionSystem system;
    private RegisterMode registerMode;
    private Date startRegister;
    private Date endRegister;
    private boolean publicVisible;
    private Long club;
    private String clubName;
    private String owner;
    private List<SimpleRegister> registers; // for SAFCA
    private boolean canEdit;
    private boolean canEditRegisters;
    private String data1;
    private String data2;
    private String data3;
    private String data4;
    private String config;
    private List<PresetData> presets;
    private List<Categorie> requiredWeight;

    public CompetitionData() {
        this(null, "", "", "", "", new Date(), new Date(),
                CompetitionSystem.INTERNAL, RegisterMode.FREE, new Date(), new Date(), true,
                null, "", "", null, true, true,
                "", "", "", "", "{}", new ArrayList<>(), new ArrayList<>());
    }

    public static CompetitionData fromModel(CompetitionModel model) {
        if (model == null)
            return null;

        return new CompetitionData(model.getId(), model.getName(), model.getDescription(), model.getAdresse(),
                model.getUuid(), model.getDate(), model.getTodate(), model.getSystem(),
                model.getRegisterMode(), model.getStartRegister(), model.getEndRegister(), model.isPublicVisible(),
                model.getClub().getId(), model.getClub().getName(), model.getOwner(), null, false, false,
                model.getData1(), model.getData2(), model.getData3(), model.getData4(), model.getConfig(),
                new ArrayList<>(), model.getRequiredWeight());
    }

    public static CompetitionData fromModelLight(CompetitionModel model) {
        if (model == null)
            return null;

        CompetitionData out = new CompetitionData(model.getId(), model.getName(), model.getDescription(),
                model.getAdresse(), "", model.getDate(), model.getTodate(), null,
                model.getRegisterMode(), model.getStartRegister(), model.getEndRegister(), model.isPublicVisible(),
                null, model.getClub().getName(), "", null, false, false,
                "", "", "", "", "{}", new ArrayList<>(), model.getRequiredWeight());

        if (model.getRegisterMode() == RegisterMode.HELLOASSO) {
            out.setData1(model.getData1());
            out.setData2(model.getData2());
        }


        return out;
    }

    public CompetitionData addInsc(List<RegisterModel> insc, List<CompetitionGuestModel> guests) {
        this.registers = Stream.concat(
                insc.stream()
                        .map(i -> new SimpleRegister(i.getMembre().getId(), i.getOverCategory(), i.getWeight2(),
                                i.getCategorie(), (i.getClub() == null) ? null : i.getClub().getId(),
                                (i.getClub() == null) ? null : i.getClub().getName())),
                guests.stream()
                        .map(i -> new SimpleRegister(i.getId() * -1, 0, i.getWeight2(),
                                i.getCategorie(), null, i.getClub()))).toList();
        return this;
    }

    public CompetitionData addPresets(List<CatPresetModel> presets) {
        this.presets = presets.stream().map(PresetData::fromModel).toList();
        return this;
    }

    @Data
    @AllArgsConstructor
    @RegisterForReflection
    public static class SimpleRegister {
        long id;
        int overCategory;
        Float weight;
        Categorie categorie;
        Long club;
        String club_str;
    }
}
