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

import SwiftUI

struct MatchSummaryView: View {
    var match: Match
    let matchViewStyle: MatchViewStyle
    let matchTitle: String
    let roundTitle: String?
    let courtName: String?
    let spacing: CGFloat
    let padding: CGFloat
    let color: Color
    let width: CGFloat

    init(match: Match, matchViewStyle: MatchViewStyle) {
        self.match = match
        self.matchViewStyle = matchViewStyle
        self.padding = matchViewStyle == .plainStyle ? 0 : 8
        self.spacing = matchViewStyle == .plainStyle ? 8 : 0
        self.width = matchViewStyle == .plainStyle ? 1 : 2
        self.color = Color(white: 0.9)

        if let groupStage = match.groupStageObject {
            self.roundTitle = groupStage.groupStageTitle()
        } else if let round = match.roundObject {
            self.roundTitle = round.roundTitle(.short)
        } else {
            self.roundTitle = nil
        }
        
        self.matchTitle = match.matchTitle(.short)
        
        if let court = match.courtName(), match.hasEnded() == false {
            self.courtName = court
        } else {
            self.courtName = nil
        }
    }
    
    var body: some View {
        VStack(alignment: .leading) {
            if matchViewStyle != .plainStyle {
                if matchViewStyle == .feedStyle, let tournament = match.currentTournament() {
                    Text(tournament.tournamentTitle())
                }
                HStack {
                    if matchViewStyle != .sectionedStandardStyle {
                        if let roundTitle {
                            Text(roundTitle).fontWeight(.semibold)
                        }
                        Text(matchTitle)
                    }
                    Spacer()
                    if let courtName {
                        Spacer()
                        Text(courtName)
                            .foregroundStyle(.gray)
                            .font(.caption)
                    }
                }
                .lineLimit(1)
            }
            
            HStack(spacing: 0) {
                VStack(alignment: .leading, spacing: spacing) {
                    PlayerBlockView(match: match, teamPosition: .one, color: color, width: width)
                        .padding(padding)
                    if width == 1 {
                        Divider()
                    } else {
                        Divider().frame(height: width).overlay(color)
                    }
                    PlayerBlockView(match: match, teamPosition: .two, color: color, width: width)
                        .padding(padding)
                }
            }
            .overlay {
                if matchViewStyle != .plainStyle {
                    RoundedRectangle(cornerRadius: 8)
                        .stroke(color, lineWidth: 2)
                }
            }
            
            if matchViewStyle != .plainStyle {
                HStack {
                    Spacer()
                    MatchDateView(match: match, showPrefix: matchViewStyle == .tournamentResultStyle)
                }
            }
        }
        .padding(.vertical, padding)
        .monospacedDigit()
    }
}

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