//
//  PreviewBracketPositionView.swift
//  PadelClub
//
//  Created by razmig on 23/10/2024.
//

import SwiftUI

struct PreviewBracketPositionView: View {
    let seeds: [TeamRegistration]
    let drawLogs: [DrawLog]

    @State private var filterOption: PreviewBracketPositionFilterOption = .difference
    
    enum PreviewBracketPositionFilterOption: Int, Identifiable, CaseIterable {
        var id: Int { self.rawValue }
        case all
        case difference
        case summon
        
        func isDisplayable(_ team: TeamRegistration, drawLog: DrawLog?) -> Bool {
            switch self {
            case .all:
                true
            case .difference:
                team.isDifferentPosition(drawLog?.computedBracketPosition()) == true
            case .summon:
                team.callDate != drawLog?.drawMatch()?.startDate
            }
        }
    }
    
    var body: some View {
        List {
            Section {
                ForEach(seeds.indices, id: \.self) { seedIndex in
                    let seed = seeds[seedIndex]
                    let drawLog = drawLogs.first(where: { $0.drawSeed == seedIndex })
                    if filterOption.isDisplayable(seed, drawLog: drawLog) {
                        HStack {
                            VStack(alignment: .leading) {
                                Text("Tête de série #\(seedIndex + 1)").font(.caption)
                                TeamRowView.TeamView(team: seed)
                                TeamRowView.TeamCallDateView(team: seed)
                            }
                            Spacer()
                            if let drawLog {
                                VStack(alignment: .trailing) {
                                    Text(drawLog.roundLabel()).lineLimit(1).truncationMode(.middle).font(.caption)
                                    Text(drawLog.matchLabel()).lineLimit(1).truncationMode(.middle)
                                    Text(drawLog.localizedDrawBranch())
                                    if let expectedDate = drawLog.drawMatch()?.startDate {
                                        Text(expectedDate.localizedDate())
                                            .font(.caption)
                                    } else {
                                        Text("Aucun horaire")
                                            .font(.caption)
                                    }
                                }
                            }
                        }
                        .listRowView(isActive: true, color: seed.isDifferentPosition(drawLog?.computedBracketPosition()) ? .logoRed : .green, hideColorVariation: true)
                    }
                }
            } header: {
                Picker(selection: $filterOption) {
                    Text("Tous").tag(PreviewBracketPositionFilterOption.all)
                    Text("Changements").tag(PreviewBracketPositionFilterOption.difference)
                    Text("Convoc.").tag(PreviewBracketPositionFilterOption.summon)
                } label: {
                    Text("Filter")
                }
                .labelsHidden()
                .pickerStyle(.segmented)
                .textCase(nil)
            }
        }
        .overlay(content: {
            if seeds.isEmpty {
                ContentUnavailableView("Aucune équipe", systemImage: "person.2.slash", description: Text("Aucun tête de série dans le tournoi."))
            } else if filterOption == .difference, noSeedHasDifferentPlace() {
                ContentUnavailableView("Aucun changement", systemImage: "dice", description: Text("Aucun changement dans le tableau."))
            } else if filterOption == .summon, noSeedHasDifferentSummon() {
                ContentUnavailableView("Aucun changement", systemImage: "dice", description: Text("Aucun changement dans le tableau."))
            }
        })
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
        .navigationTitle("Aperçu du tableau tiré")

    }
    
    func noSeedHasDifferentPlace() -> Bool {
        seeds.enumerated().allSatisfy({ seedIndex, seed in
            let drawLog = drawLogs.first(where: { $0.drawSeed == seedIndex })
            return seed.isDifferentPosition(drawLog?.computedBracketPosition()) == false
        })
    }
    
    func noSeedHasDifferentSummon() -> Bool {
        seeds.enumerated().allSatisfy({ seedIndex, seed in
            let drawLog = drawLogs.first(where: { $0.drawSeed == seedIndex })
            return seed.callDate == drawLog?.drawMatch()?.startDate
        })
    }
}
