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

import SwiftUI

enum EcaAlertType: String {
    case ecaDelete
    case ecaRunningStop
}

struct TaskSwitchingMenuView: View {
    @Binding var viewMode: TaskViewMode
    @Binding var edittingEcaArea: RegisteredEca?
    @Binding var isShowSettingEca : Bool
    @ObservedObject var ecaData = SharingData.eca
    @State var isEcaAlert : Bool = false
    @State var ecaAlertType: EcaAlertType = .ecaDelete
    let deleteEcaArea = DeleteEcaArea()

    var body: some View {
        VStack{
            ForEach(ecaData.ecaArea.map{ $0.1 }.filter{ $0.isEnable }.sorted{ $0.id ?? 0 < $1.id ?? 0 }, id: \.name){ eca in
                VStack {
                    HStack {
                        //ECA開始・終了ボタン
                        Button {
                            for runningEca in ecaData.ecaArea.map{ $0.1 }.filter{ $0.isRunning }{
                                edittingEcaArea = runningEca
                                isEcaAlert = true
                                ecaAlertType = .ecaRunningStop
                            }
                            if isEcaAlert == false {
                                var newData = eca
                                newData.isRunning = true
                                newData.status = EcaState.running
                                SharingData.eca.editEcaArea(key: eca.name, value: newData, type: EcaOperation.Running)
                            }
                        } label: {
                            HStack {
                                Circle()
                                    .frame(width: 14, height: 14)
                                    .foregroundColor(eca.isRunning ? ColorSet.PrimaryActiveIcon.color : ColorSet.TaskStateIcon.color)
                                Spacer()
                                    .frame(width: 10)
                            }
                        }

                        Text(eca.name)
                            .font(FontStyle.DefaultText.font)
                            .foregroundColor(ColorSet.Body.color)
                            .onTapGesture {
                                ecaData.focusEca = eca.name
                            }
                        
                        Spacer()
                        
                        Menu {
                            Text(eca.name)

                            Button{
                                edittingEcaArea = eca
                                isShowSettingEca = true
                            } label: {
                                Text("Edit Notice Setting")
                            }
                            
                            Button{
                                edittingEcaArea = eca
                                isEcaAlert = true
                                ecaAlertType = .ecaDelete
                            } label: {
                                Text("Delete ECA Task")
                            }
                        } label: {
                            Image(systemName: "ellipsis")
                                .frame(width: 22, height: 22)
                                .foregroundColor(eca.isRunning ? ColorSet.Splash.color : ColorSet.Slidebar.color)
                        }
                        .disabled(eca.isRunning)
                        .alert(isPresented: $isEcaAlert) {
                            switch ecaAlertType {
                            case .ecaDelete:
                                return Alert(title: Text("delete"),
                                             message: Text("Do you delete " + eca.name),
                                             primaryButton: .default(Text("cancel")),
                                             secondaryButton: .cancel(Text("delete"), action: {
                                    if let ecaArea = edittingEcaArea {
                                        var newData = ecaArea
                                        newData.isEnable = false
                                        newData.status = EcaState.register

                                        ecaData.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.Delete)
                                        deleteEcaArea.start(ecaId: ecaArea.id)
                                    }
                                    edittingEcaArea = nil
                                }))
                            case .ecaRunningStop:
                                return Alert(title: Text("stop"),
                                             message: Text("Do you want to stop " + eca.name),
                                             primaryButton: .default(Text("cancel")),
                                             secondaryButton: .cancel(Text("stop"), action: {
                                    if let ecaArea = edittingEcaArea {
                                        var newData = ecaArea
                                        newData.isRunning = false
                                        newData.status = EcaState.cancel
                                        ecaData.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.Cancel)
                                    }
                                    edittingEcaArea = nil
                                }))
                            }
                        }
                    }
                    .frame(height: 60)
                    .padding(.horizontal, 30)
                    
                    Divider()
                        .background(.white)
                }
            }
            //AddECAボタン
            Button(action: {
                viewMode = .EcaList
            }, label: {
                Text("Add ECA Task")
                    .font(FontStyle.DefaultText.font)
                    .padding()
            })
            .frame(width: 153, height: 42)
            .foregroundColor(ColorSet.ButtonText.color)
            .background(ColorSet.PrimaryActiveIcon.color)
            .cornerRadius(30)
            
            Spacer()
        }
//        .padding()
    }
}


#Preview {
    TaskSwitchingMenuView(viewMode: .constant(.SwitchingMenu), edittingEcaArea: .constant(nil), isShowSettingEca: .constant(false))
}
