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

import SwiftUI
import UIKit

struct EcaSettingView: View {
    @ObservedObject var taskViewModel: TaskViewModel
    @State var edittingEca: RegisteredEca

    var body: some View {
        VStack(spacing: 0){
            SwSliderView(data: $edittingEca, type: .Finish)
                .padding(.horizontal, 30)
                .padding(.vertical, 17)
            Divider()
                .frame(height: 1)
                .background(ColorSet.LineColor04.color)

            SwSliderView(data: $edittingEca, type: .Start)
                .padding(.horizontal, 30)
                .padding(.vertical, 17)
            Divider()
                .frame(height: 1)
                .background(ColorSet.LineColor04.color)

            SwSliderView(data: $edittingEca, type: .Notice)
                .padding(.horizontal, 30)
                .padding(.vertical, 17)
            Divider()
                .frame(height: 1)
                .background(ColorSet.LineColor04.color)

            Button(action: {
                var ecaData = edittingEca
                ecaData.isEnable = true
                SharingData.eca.editEcaArea(key: edittingEca.areaId, value: ecaData, type: EcaOperation.Change)
                taskViewModel.viewMode = .FuelSwitching
            }, 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)
            .disabled(!checkSwitchingDistance())
        }
    }
    
    private func checkSwitchingDistance() -> Bool{
        let notice = edittingEca.swNotice
        let start = edittingEca.swStart
        let finish = edittingEca.swFinish

        return notice > start && notice > finish && start > finish
    }
    
    
    struct SwSliderView: View {
        @Binding var data : RegisteredEca
        @Binding var param : Float
        var type: SwType
        
        var min : Float = 1
        var max : Float = 10
        
        init(data: Binding<RegisteredEca>, type: SwType) {
            let thumb = UIImage(systemName: "circle.fill")
            UISlider.appearance().setThumbImage(thumb, for: .normal)
            UISlider.appearance().maximumTrackTintColor = UIColor(ColorSet.Slidebar.color)
            self._data = data
            self._param = type == .Notice ? data.swNotice : type == .Start ? data.swStart : data.swFinish
            self.type = type
            switch type{
            case .Finish:
                self.max = data.swStart.wrappedValue <= 2 ? 2 : data.swStart.wrappedValue - 1
            case .Start:
                self.min = data.swFinish.wrappedValue
                self.max = data.swNotice.wrappedValue
            case .Notice:
                self.min = data.swStart.wrappedValue >= 9 ? 9 : data.swStart.wrappedValue + 1
            }
        }
        enum SwType: String{
            case Notice
            case Start
            case Finish
            
            var title: String {
                switch self {
                case .Notice:
                    "Switching Notice - ECA"
                case .Start:
                    "Switching Start - ECA"
                case .Finish:
                    "Switching Finish - ECA"
                }
            }
        }
        var body: some View {
            VStack(alignment: .leading){
                Text(type.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: {
                        switch type{
                        case .Finish:
                            break
                        case .Start:
                            if data.swFinish >= param - 1 && param > 2{
                                data.swFinish -= 1
                            }
                            if param <= 2{
                                return
                            }
                        case .Notice:
                            if data.swNotice - data.swStart <= 1{
                                data.swStart -= 1
                                if data.swStart <= data.swFinish{
                                    data.swFinish = data.swStart - 1
                                }
                            }
                        }
                        param -= 1
                        
                    }, label: {
                        Image(systemName: "minus.circle")
                            .resizable()
                            .frame(width: 28, height: 28)
                    })
                    .disabled(param <= min)
                    
                    Slider(value: $param, in: min...max, step: 1){_ in
                        if type == .Start{
                            if param == min{
                                param += 1
                            }
                            if param == max{
                                param -= 1
                            }
                        }else if type == .Notice{
                            if param <= data.swStart{
                                data.swStart = param - 1
                                if data.swStart <= data.swFinish{
                                    data.swFinish = data.swStart - 1
                                }
                            }
                        }else if type == .Finish{
                            if param >= data.swStart{
                                data.swStart = param + 1
                                if data.swStart >= data.swNotice{
                                    data.swNotice = data.swStart + 1
                                }
                            }
                        }
                        
                    }
                                    
                    Button(action: {
                        switch type{
                        case .Finish:
                            if data.swStart - data.swFinish <= 1{
                                data.swStart += 1
                                if data.swStart >= data.swNotice{
                                    data.swNotice = data.swStart + 1
                                }
                            }
                        case .Start:
                            if data.swNotice <= param + 1 && param < 9{
                                data.swNotice += 1
                            }
                            if param >= 9{
                                return
                            }
                        case .Notice:
                            break
                        }
                        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(taskViewModel: TaskViewModel(), edittingEca: RegisteredEca(id: 0, areaName: "Alert Area")!)
}
