//
//  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
    
    var body: some View {
        VStack{
            ForEach(ecaData.ecaArea.map{ $0.1 }.filter{ $0.isEnable }, id: \.name){ eca in
                VStack {
                    HStack {
                        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
                                ecaData.editEcaArea(key: eca.name, value: newData)
                            }
                        } 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.foucusEca = 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("eca delete"),
                                             primaryButton: .default(Text("cancel")),
                                             secondaryButton: .cancel(Text("delete"), action: {
                                    if let ecaArea = edittingEcaArea {
                                        var newData = ecaArea
                                        newData.isEnable = false
                                        ecaData.editEcaArea(key: ecaArea.name, value: newData)
                                    }
                                    edittingEcaArea = nil
                                }))
                            case .ecaRunningStop:
                                return Alert(title: Text("stop"),
                                             message: Text("eca running stop"),
                                             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)
                                        //サーバーにECAキャンセルを通知
                                    }
                                    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))
}
