//
//  MatchSummaryView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 23/03/2024.
//

import SwiftUI

struct MatchSummaryView: View {
    @EnvironmentObject var dataStore: DataStore
    @State var match: Match
    @Environment(\.matchViewStyle) private var matchViewStyle
    let matchTitle: String
    let roundTitle: String?
    let courtName: String?
    let updatedField: Int?
    let estimatedStartDate: Match.CourtIndexAndDate?
    let availableCourts: [Int]
    let canBePlayedInSpecifiedCourt: Bool

    init(match: Match, title: String? = nil, updatedField: Int? = nil) {
        self.match = match
        self.updatedField = updatedField
        
        let runningMatches = DataStore.shared.runningMatches()

        let currentAvailableCourts = match.availableCourts(runningMatches: runningMatches)
        self.availableCourts = currentAvailableCourts

        if let groupStage = match.groupStageObject {
            self.roundTitle = groupStage.groupStageTitle(.title)
        } else if let round = match.roundObject {
            self.roundTitle = round.roundTitle(.short)
        } else {
            self.roundTitle = nil
        }
        
        self.matchTitle = title ?? match.matchTitle(.short)
        
        if match.hasEnded() == false, let court = match.courtName() {
            self.courtName = court
        } else {
            self.courtName = nil
        }
        self.estimatedStartDate = match.estimatedStartDate(availableCourts: currentAvailableCourts, runningMatches: runningMatches)
        self.canBePlayedInSpecifiedCourt = match.canBePlayedInSpecifiedCourt(runningMatches: runningMatches)
    }
    
    var spacing: CGFloat {
        matchViewStyle == .plainStyle ? 8 : 0
    }
    
    var padding: CGFloat {
        matchViewStyle == .plainStyle ? 0 : 8
    }
    
    var width: CGFloat {
        matchViewStyle == .plainStyle ? 1 : 2
    }

    var body: some View {
        VStack(alignment: .leading) {
            if matchViewStyle != .plainStyle {
                HStack {
                    if matchViewStyle != .sectionedStandardStyle {
                        if let roundTitle {
                            Text(roundTitle).fontWeight(.semibold)
                        }
                        if match.groupStage != nil || match.index > 0 {
                            Text(matchTitle)
                        }
                    }
                    Spacer()
                    VStack(alignment: .trailing, spacing: 0) {
                        if let courtName {
                            Text(courtName)
                                .strikethrough(courtIsNotValid())
                        }
                    }
                    .foregroundStyle(.secondary)
                    .font(.footnote)
                }
                .lineLimit(1)
            }
            
            HStack(spacing: 0) {
                VStack(alignment: .leading, spacing: spacing) {
                    PlayerBlockView(match: match, teamPosition: .one)
                        .padding(padding)
                    if width == 1 {
                        Divider()
                    } else {
                        Divider().frame(height: width).overlay(Color(white: 0.9))
                    }
                    PlayerBlockView(match: match, teamPosition: .two)
                        .padding(padding)
                }
            }
            .overlay {
                if matchViewStyle != .plainStyle {
                    RoundedRectangle(cornerRadius: 8)
                        .stroke(Color(white: 0.9), lineWidth: 2)
                }
            }
            
            if matchViewStyle != .plainStyle {
                HStack {
                    if matchViewStyle == .followUpStyle {
                        if match.expectedToBeRunning() {
                            Text(match.expectedFormattedStartDate(canBePlayedInSpecifiedCourt: canBePlayedInSpecifiedCourt, availableCourts: availableCourts, estimatedStartDate: estimatedStartDate, updatedField: updatedField))
                                .font(.footnote)
                                .foregroundStyle(.secondary)
                        }
                    }
                    Spacer()
                    MatchDateView(match: match, showPrefix: false, updatedField: possibleCourtIndex)
                }
            }
        }
        .padding(.vertical, padding)
        .monospacedDigit()
    }
    
    var possibleCourtIndex: Int? {
        if canBePlayedInSpecifiedCourt {
            return nil
        } else if let updatedField, availableCourts.contains(updatedField) {
            return updatedField
        } else if let first = availableCourts.first {
            return first
        } else if let estimatedStartDate {
            return estimatedStartDate.0
        }
        return updatedField
    }
    
    func courtIsNotValid() -> Bool {
        if match.courtIndex == updatedField {
            return false
        }
        
        if match.isReady() == false {
            return false
        }
        
        if canBePlayedInSpecifiedCourt {
            return false
        }
        
        if let estimatedStartDate, estimatedStartDate.0 == updatedField {
            return false
        }
        
        if let estimatedStartDate, estimatedStartDate.0 == match.courtIndex {
            return false
        }


        return true
    }
}

//#Preview {
//    MatchSummaryView(match: Match.mock(), matchViewStyle: .standardStyle)
//}
