//
//  EcaSettingView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/18.
//

import SwiftUI

struct EcaSettingView: View {
    @Binding var isShowSettingEca: Bool
    @State var edittingEca: RegisteredEca
    
    var body: some View {
        VStack{
            SliderView(param: $edittingEca.swFinish, title: "Switching Finish - ECA")
            Divider()
            SliderView(param: $edittingEca.swStart, title: "Switching Start - ECA")
            Divider()
            SliderView(param: $edittingEca.swNotice, title: "Switching Notice - ECA")
            
            
            Button(action: {
                SharingData.shared.editEcaArea(key: edittingEca.name, value: edittingEca)
                isShowSettingEca = false
            }, label: {
                Text("Register")
                    .padding()
            })
            .background(ColorSet.PrimaryActiveIcon.color)
            .cornerRadius(30)
        }
        
        .padding(.horizontal)
    }
}

fileprivate struct SliderView: View {
    @Binding var param: Float
    let title: String
    var min : Float = 1
    var max : Float = 10
    var body: some View {
        VStack(alignment: .leading){
            Text(title)
                .foregroundColor(ColorSet.Body.color)

            HStack{
                Text(String(format: "%d", Int(param)))
                Text("NM")
            }
            .foregroundColor(ColorSet.Body.color)
            HStack{
                Button(action: {
                    param -= 1
                }, label: {
                    Image(systemName: "minus.circle")
                })
                .disabled(param <= min)
                Slider(value: $param, in: min...max, step: 1)
                
                Button(action: {
                    
                    param += 1
                }, label: {
                    Image(systemName: "plus.circle")
                })
                .disabled(param >= max)
            }
        }
    }
}

#Preview {
    EcaSettingView(isShowSettingEca: .constant(true), edittingEca: RegisteredEca(ecaName: "eca")!)
}
