package fr.titionfire.ffsaf.rest.data;

import fr.titionfire.ffsaf.data.model.MatchModel;
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.HashMap;
import java.util.List;

@Data
@NoArgsConstructor
@RegisterForReflection
public class ResultCategoryData {
    int type;
    String name;
    HashMap<Character, List<PouleArrayData>> matchs = new HashMap<>();
    HashMap<Character, List<RankArray>> rankArray = new HashMap<>();
    ArrayList<TreeNode<TreeData>> trees;

    @Data
    @AllArgsConstructor
    @RegisterForReflection
    public static class RankArray {
        int rank;
        String name;
        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 end) {
        public static PouleArrayData fromModel(MatchModel matchModel) {
            return new PouleArrayData(
                    matchModel.getC1Name(),
                    matchModel.isEnd() && matchModel.win() > 0,
                    matchModel.isEnd() ?
                            matchModel.getScores().stream().map(s -> new Integer[]{s.getS1(), s.getS2()}).toList()
                            : new ArrayList<>(),
                    matchModel.isEnd() && matchModel.win() < 0,
                    matchModel.getC2Name(),
                    matchModel.isEnd());
        }
    }

    @RegisterForReflection
    public static record TreeData(long id, String c1FullName, String c2FullName, List<ScoreEmbeddable> scores,
                                  boolean end) {
        public static TreeData from(MatchModel match) {
            return new TreeData(match.getId(), match.getC1Name(), match.getC2Name(), match.getScores(), match.isEnd());
        }
    }
}
