//
//  MatchFormatStorageView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 18/04/2024.
//

import SwiftUI
import LeStorage

struct MatchFormatStorageView: View {
    @State private var estimatedDuration: Int
    @EnvironmentObject var dataStore: DataStore
    
    let matchFormat: MatchFormat
    
    init(matchFormat: MatchFormat) {
        self.matchFormat = matchFormat
        _estimatedDuration = State(wrappedValue: matchFormat.getEstimatedDuration())
    }
    
    var body: some View {
        Section {
            LabeledContent {
                StepperView(title: "minutes", count: $estimatedDuration, step: 5)
            } label: {
                Text("Durée \(matchFormat.format)")
                Text(matchFormat.computedShortLabelWithoutPrefix)
            }
        } footer: {
            if estimatedDuration != matchFormat.defaultEstimatedDuration {
                HStack {
                    Spacer()
                    Button {
                        self.estimatedDuration = matchFormat.defaultEstimatedDuration
                    } label: {
                        Text("remettre la durée par défault")
                            .underline()
                    }
                    .buttonStyle(.borderless)
                    
                }
            }
        }
        .onChange(of: estimatedDuration) {
            dataStore.user.saveMatchFormatsDefaultDuration(matchFormat, estimatedDuration: estimatedDuration)
            self.dataStore.saveUser()
        }
    }
}

