package fr.titionfire.ffsaf.rest.data;

import com.fasterxml.jackson.annotation.JsonIgnore;
import fr.titionfire.ffsaf.data.model.CombModel;
import fr.titionfire.ffsaf.data.model.MembreModel;
import fr.titionfire.ffsaf.domain.entity.MatchModelExtend;
import fr.titionfire.ffsaf.utils.ResultPrivacy;
import fr.titionfire.ffsaf.utils.ScoreEmbeddable;
import fr.titionfire.ffsaf.utils.TreeNode;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Data
@NoArgsConstructor
@RegisterForReflection
public class ResultCategoryData {
    int type;
    String name;
    boolean treeIsClassement;
    HashMap<Character, List<PouleArrayData>> matchs = new HashMap<>();
    HashMap<Character, List<RankArray>> rankArray = new HashMap<>();
    ArrayList<TreeNode<TreeData>> trees;
    List<ClassementData> classement = new ArrayList<>();
    String[] liceName;
    long genTime;

    @Data
    @AllArgsConstructor
    @RegisterForReflection
    public static class RankArray {
        int rank;
        @JsonIgnore
        CombModel comb;
        String name;
        int score;
        int win;
        int pointMake;
        int pointTake;
        float pointRate;
    }

    @RegisterForReflection
    public record PouleArrayData(String red, boolean red_w, List<Integer[]> score, boolean blue_w, String blue,
                                 boolean eq, boolean end, Date date) {
        public static PouleArrayData fromModel(MatchModelExtend matchModel, MembreModel membreModel,
                                               ResultPrivacy privacy) {
            return new PouleArrayData(
                    matchModel.getC1Name(membreModel, privacy),
                    matchModel.isEnd() && matchModel.getWin() > 0,
                    matchModel.isEnd() ?
                            matchModel.getScoresToPrint().stream().map(s -> new Integer[]{s.getS1(), s.getS2()})
                                    .toList()
                            : new ArrayList<>(),
                    matchModel.isEnd() && matchModel.getWin() < 0,
                    matchModel.getC2Name(membreModel, privacy),
                    matchModel.isEnd() && matchModel.getWin() == 0,
                    matchModel.isEnd(),
                    matchModel.getDate());
        }
    }

    @RegisterForReflection
    public static record TreeData(long id, String c1FullName, String c2FullName, List<ScoreEmbeddable> scores,
                                  boolean end, int win, int level, Date date) {
        public static TreeData from(MatchModelExtend match, int level, MembreModel membreModel, ResultPrivacy privacy) {
            return new TreeData(match.getId(), match.getC1Name(membreModel, privacy),
                    match.getC2Name(membreModel, privacy), match.getScoresToPrint(), match.isEnd(), match.getWin(),
                    level, match.getDate());
        }
    }

    @RegisterForReflection
    public static record ClassementData(int rank, @JsonIgnore CombModel comb, String name) {

    }
}
