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

import SwiftUI
import UIKit

struct EcaSettingView: View {
    @Binding var isShowSettingEca: Bool
    @State var edittingEca: RegisteredEca
    
    var body: some View {
        VStack(spacing: 0){
            SliderView(param: $edittingEca.swFinish, title: "Switching Finish - ECA")
                .padding(.horizontal, 30)
                .padding(.vertical, 17)
            Divider()
            SliderView(param: $edittingEca.swStart, title: "Switching Start - ECA")
                .padding(.horizontal, 30)
                .padding(.vertical, 17)
            Divider()
            SliderView(param: $edittingEca.swNotice, title: "Switching Notice - ECA")
                .padding(.horizontal, 30)
                .padding(.vertical, 17)
            
            
            Button(action: {
                SharingData.eca.editEcaArea(key: edittingEca.name, value: edittingEca)
                isShowSettingEca = false
            }, label: {
                Text("Register")
                    .font(FontStyle.DefaultText.font)
                    .padding()
            })
            .frame(width: 153, height: 42)
            .foregroundColor(ColorSet.ButtonText.color)
            .background(ColorSet.PrimaryActiveIcon.color)
            .cornerRadius(30)
            .padding(.vertical, 20)
        }
    }
}

fileprivate struct SliderView: View {
    init(param: Binding<Float>, title: String, min: Float = 1, max: Float = 10) {
        let thumb = UIImage(systemName: "circle.fill")
        UISlider.appearance().setThumbImage(thumb, for: .normal)
        UISlider.appearance().maximumTrackTintColor = UIColor(ColorSet.Slidebar.color)
        self._param = param
        self.title = title
        self.min = min
        self.max = max
    }
    @Binding var param: Float
    let title: String
    var min : Float = 1
    var max : Float = 10
    var body: some View {
        VStack(alignment: .leading){
            Text(title)
                .font(FontStyle.EmphasisText.font)

            HStack(alignment: .bottom){
                Text(String(format: "%d", Int(param)))
                    .font(.system(size: 20))
                Text("NM")
                    .font(FontStyle.SupplementText2.font)
                    .foregroundColor(ColorSet.BodyDescriptiion
                        .color)
            }
            
            HStack(spacing: 9){
                Button(action: {
                    param -= 1
                }, label: {
                    Image(systemName: "minus.circle")
                        .resizable()
                        .frame(width: 28, height: 28)
                })
                .disabled(param <= min)
                
                Slider(value: $param, in: min...max, step: 1)
                                
                Button(action: {
                    
                    param += 1
                }, label: {
                    Image(systemName: "plus.circle")
                        .resizable()
                        .frame(width: 28, height: 28)
                })
                .disabled(param >= max)
                
                Spacer()
            }
            .tint(ColorSet.SlidebarActive.color)
            .foregroundColor(ColorSet.SlidebarActive.color)
        }
        .foregroundColor(ColorSet.Body.color)
    }
}

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