//
//  EditScoreView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 27/02/2023.
//

import SwiftUI
import LeStorage

struct EditScoreView: View {
    @EnvironmentObject var dataStore: DataStore
    @ObservedObject var matchDescriptor: MatchDescriptor
    @Environment(\.dismiss) private var dismiss

    func walkout(_ team: TeamPosition) {
        self.matchDescriptor.match?.setWalkOut(team)
        save()
        dismiss()
    }
    
    var body: some View {
        Form {
            Section {
                Text(matchDescriptor.teamLabelOne)
                HStack {
                    Spacer()
                    Text(matchDescriptor.teamLabelTwo).multilineTextAlignment(.trailing)
                }
            } footer: {
                HStack {
                    Menu {
                        Button {
                            walkout(.one)
                        } label: {
                            Text(matchDescriptor.teamLabelOne)
                        }
                        Button {
                            walkout(.two)
                        } label: {
                            Text(matchDescriptor.teamLabelTwo)
                        }
                    } label: {
                        Text("Forfait d'une équipe ?")
                            .underline()
                    }
                    Spacer()

                    MatchTypeSmallSelectionView(selectedFormat: $matchDescriptor.matchFormat, format: "Format")
                        .onChange(of: matchDescriptor.matchFormat) {
                            matchDescriptor.setDescriptors.removeAll()
                            matchDescriptor.addNewSet()
                        }
                }
            }
            ForEach($matchDescriptor.setDescriptors) { $setDescriptor in
                SetInputView(setDescriptor: $setDescriptor)
                    .onChange(of: setDescriptor.hasEnded) {
                        if setDescriptor.hasEnded {
                            if matchDescriptor.hasEnded == false {
                                matchDescriptor.addNewSet()
                            }
                        } else {
                            let index = matchDescriptor.setDescriptors.firstIndex(where: { $0 == setDescriptor }) ?? 0
                            matchDescriptor.setDescriptors = Array(matchDescriptor.setDescriptors[0...index])
                        }
                    }
            }
            
            if matchDescriptor.hasEnded {
                Section {
                    HStack {
                        Spacer()
                        VStack {
                            Text(matchDescriptor.winnerLabel)
                        }
                        .multilineTextAlignment(.center)
                        Spacer()
                    }
                    RowButtonView("Victoire") {
                        matchDescriptor.match?.setScore(fromMatchDescriptor: matchDescriptor)
                        save()
                        dismiss()
                    }
                } footer: {
                    Text("Termine la rencontre sur ce score")
                }
            }
            
            if matchDescriptor.match?.hasEnded() == false {
                Section {
                    RowButtonView("Mise à jour") {
                        matchDescriptor.match?.updateScore(fromMatchDescriptor: matchDescriptor)
                        save()
                        dismiss()
                    }
                } footer: {
                    Text("Met à jour le score, ne termine pas la rencontre")
                }
            }
        }
    }
    
    func save() {
        if let match = matchDescriptor.match {
            do {
                try dataStore.matches.addOrUpdate(instance: match)
            } catch {
                Logger.error(error)
            }
        }
    }
}
