//
//  SharingData.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/20.
//
import Foundation
import CoreLocation

class SharingData{
    
    static var my = My()
    class My: ObservableObject {
        @Published var id: Int = 0
        @Published var shipId: Int = 0
        @Published var shipName: String = ""
        @Published var imo: Int = 0
        @Published var mmsi: Int = 0
        @Published var isUpDate: Bool = false
        @Published var isFuelSwitchTask: Bool = false

        @Published var ecaStatus: EcaState?

        var manualUrls: [ResGetManualUrl] = []
    }

    static var location = Location()
    class Location: ObservableObject {
        @Published var location: CLLocationCoordinate2D? = .init(latitude: 0, longitude: 0)
        @Published var gps: CLLocationCoordinate2D? = nil
        @Published var server: CLLocationCoordinate2D? = nil

        @Published var status: ShipAlert = ShipAlert.Normal
        @Published var totalRmsAlarm: AlertStatusType = AlertStatusType.Normal
        @Published var speed: Double = 0.0
        @Published var course: Double = 0.0
        @Published var heading: Double = 0.0
        @Published var dataTime: String = "" //2023-11-02T05:25:49.4362123Z

        @Published var focusOwnShip: Bool = false

        func setLocation(){
            if Preferences.LocationType == 0 {
                self.location = self.gps
            } else {
                self.location = self.server
            }
        }

        func setShipStatus(status: Int) -> ShipAlert {
            var shipAlert = ShipAlert.Normal

            switch status {
            case -1:
                shipAlert = ShipAlert.Offline
            case 0:
                shipAlert = ShipAlert.Unknown
            case 1:
                shipAlert = ShipAlert.Normal
            case 2:
                shipAlert = ShipAlert.Caution
            case 3:
                shipAlert = ShipAlert.Warrning
            case 4:
                shipAlert = ShipAlert.Alarm
            case 5:
                shipAlert = ShipAlert.Emergency
            case 6:
                shipAlert = ShipAlert.NecstEmergency
            default:
                shipAlert = ShipAlert.Unknown
            }

            return shipAlert
        }

        func setTotalRmsAlarm(alarm: Int) -> AlertStatusType {
            var rmsAlarm = AlertStatusType.Normal

            switch alarm {
            case 0:
                rmsAlarm = AlertStatusType.Unknown
            case 1:
                rmsAlarm = AlertStatusType.Normal
            case 2:
                rmsAlarm = AlertStatusType.Caution
            case 3:
                rmsAlarm = AlertStatusType.Warrning
            case 4:
                rmsAlarm = AlertStatusType.Alarm
            case 5:
                rmsAlarm = AlertStatusType.Emergency
            default:
                rmsAlarm = AlertStatusType.Unknown
            }

            return rmsAlarm
        }

        func reset() {
            speed = 0.0
            course = 0.0
            heading = 0.0

            location = nil
        }
    }

    static var map = Map()
    class Map: ObservableObject {
        @Published var wayPoints: [CLLocationCoordinate2D] = []
        @Published var legLine: [CLLocationCoordinate2D] = []
        @Published var portLine: [CLLocationCoordinate2D] = []
        @Published var starboardLine: [CLLocationCoordinate2D] = []
    }

    static var message = Message()
    class Message: ObservableObject {
        @Published var mode: Bool = false   // false:通常 , true:Warning中
        @Published var messages: [ChatMessage] = []
        @Published var users: [ChatUser] = []

        func changeMode(){
            self.mode.toggle()
        }
    }

    static var information = Information()
    class Information: ObservableObject {
        @Published var content: [ResInformation] = []
    }

    /**
     * Eca
     */
    static var eca = Eca()
    class Eca: ObservableObject{
        @Published var ecaArea: Dictionary<Int, RegisteredEca> = [:]
        @Published var focusEca: Int? = nil
        @Published var isShowEcaAlert: Bool = false
        
        func setEcaArea(key: Int, value: RegisteredEca) {
            ecaArea.updateValue(value, forKey: key)
        }

        func editEcaArea(key: Int, value: RegisteredEca, type: EcaOperation) {
            let setEcaArea = SetEcaArea()
            var task = ReqTaskList(Id: value.areaId)

            switch type {
            case EcaOperation.Running:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                task.status = serverRunning
                setEcaArea.start(eca: task)
            case EcaOperation.Insert:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                task.status = serverRegister
                setEcaArea.start(eca: task)
            case EcaOperation.Delete:
                let deleteEcaArea = DeleteEcaArea()
                deleteEcaArea.start(ecaId: value.areaId)
            case EcaOperation.Notice:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                if value.isEnable && value.isRunning {
                    task.status = setEcaStatus(eca: value)
                }
                setEcaArea.start(eca: task)
            case EcaOperation.Start:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                if value.isEnable && value.isRunning {
                    task.status = setEcaStatus(eca: value)
                }
                setEcaArea.start(eca: task)
            case EcaOperation.Finish:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                if value.isEnable {
                    task.status = setEcaStatus(eca: value)
                }
                setEcaArea.start(eca: task)
            case EcaOperation.End:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                if value.isEnable {
                    task.status = setEcaStatus(eca: value)
                }
                setEcaArea.start(eca: task)
            case EcaOperation.Incomplete:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                if value.isEnable {
                    task.status = setEcaStatus(eca: value)
                }
                setEcaArea.start(eca: task)
           case EcaOperation.Cancel:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                if value.isEnable {
                    task.status = setEcaStatus(eca: value)
                }
                setEcaArea.start(eca: task)
            case EcaOperation.Change:
                task.areaId = value.areaId
                task.taskName = value.name
                task.noticeRange = value.swNotice
                task.startRange = value.swStart
                task.finishRange = value.swFinish
                if value.isEnable && value.isRunning {
                    task.status = setEcaStatus(eca: value)
                }
                setEcaArea.start(eca: task)
            }
            ecaArea.updateValue(value, forKey: key)
        }

        private func setEcaStatus(eca: RegisteredEca) -> Int {
            var state = serverRegister
            switch eca.status {
            case EcaState.running:
                state = serverRunning
            case EcaState.noticePass:
                state = serverNoticePass
            case EcaState.startPass:
                state = serverStartPass
            case EcaState.finishPass:
                state = serverFinishPass
            case EcaState.end:
                state = serverEnd
            case EcaState.incomplete:
                state = serverIncomplete
            case EcaState.cancel:
                state = serverCancel
            default:
                state = serverRegister
            }

            return state
        }
    }

    static var pushHistory = PushHistory()
    class PushHistory: ObservableObject{
        @Published var pushHistoryData: Dictionary<Int, ResPushHistory> = [:]
        @Published var focusPushHistory: Int? = nil

        func setPushHistory(key: Int, value: ResPushHistory) {
            pushHistoryData.updateValue(value, forKey: key)
        }
    }

    static var noticeInformation = NoticeInformation()
    class NoticeInformation: ObservableObject {
    }
}
