//
//  EcaArea.swift
//  Sailassist
//
//  Created by 三浦薫巳 on 2023/11/26.
//

import Foundation
import CoreLocation
import SwiftUI

struct ecaInfo {
    var areaId: Int
    var name: String
    var centerPosition: CLLocationCoordinate2D
    var zoomLevel: CGFloat
}

var ecaDataTable:[ecaInfo] = [
    ecaInfo(areaId: 1, name: "Iceland", centerPosition: CLLocationCoordinate2D(latitude: 62.56528431, longitude: -18.48170601), zoomLevel: 4.0),
    ecaInfo(areaId: 11, name: "North American Atlantic Coasts", centerPosition: CLLocationCoordinate2D(latitude: 17.89000000, longitude: -79.69611111), zoomLevel: 1.0),
    ecaInfo(areaId: 12, name: "North American Hawai", centerPosition: CLLocationCoordinate2D(latitude: 15.85444444, longitude: -158.32055556), zoomLevel: 4.0),
    ecaInfo(areaId: 13, name: "North American Pacific Coasts", centerPosition: CLLocationCoordinate2D(latitude: 37.22750000, longitude: -128.67277778), zoomLevel: 1.0),
    ecaInfo(areaId: 21, name: "North Sea 1", centerPosition: CLLocationCoordinate2D(latitude: 54.00000000, longitude: -4.00000000), zoomLevel: 3.0),
    ecaInfo(areaId: 22, name: "North Sea 2", centerPosition: CLLocationCoordinate2D(latitude: 43.50000000, longitude: -4.78333333), zoomLevel: 4.0),
    ecaInfo(areaId: 31, name: "Unites States Caribbean", centerPosition: CLLocationCoordinate2D(latitude: 13.37083333, longitude: -64.70083333), zoomLevel: 4.0),
    ecaInfo(areaId: 41, name: "China Shore", centerPosition: CLLocationCoordinate2D(latitude: 12.13420000, longitude: 115.21700000), zoomLevel: 2.0),
    ecaInfo(areaId: 42, name: "China Hainan", centerPosition: CLLocationCoordinate2D(latitude: 16.21260000, longitude: 110.15700000), zoomLevel: 5.0),
    ecaInfo(areaId: 43, name: "China Yangtzi Shuifu,Yunnan", centerPosition: CLLocationCoordinate2D(latitude: 28.60531667, longitude: 104.40085000), zoomLevel: 11.0),
    ecaInfo(areaId: 44, name: "China Yangtzi Liuhekou,Jiangsu1", centerPosition: CLLocationCoordinate2D(latitude: 31.52461111, longitude: 121.37808333), zoomLevel: 10.0),
    ecaInfo(areaId: 45, name: "China Yangtzi Liuhekou,Jiangsu2", centerPosition: CLLocationCoordinate2D(latitude: 30.20172222, longitude: 121.32094444), zoomLevel: 8.0),
    ecaInfo(areaId: 46, name: "China Xijiang Nanning,Guangxi", centerPosition: CLLocationCoordinate2D(latitude: 22.75000000, longitude: 108.31303333), zoomLevel: 11.0),
    ecaInfo(areaId: 47, name: "China Xijiang Zhaoqing,Guangdong", centerPosition: CLLocationCoordinate2D(latitude: 23.08005000, longitude: 112.77791667), zoomLevel: 11.0),
    ecaInfo(areaId: 51, name: "Korea Incheon, PyeongtaekDangjin Port", centerPosition: CLLocationCoordinate2D(latitude: 36.88996667, longitude: 126.05833333), zoomLevel: 7.0),
    ecaInfo(areaId: 52, name: "Korea Yeosu, Gwangyang Port", centerPosition: CLLocationCoordinate2D(latitude: 34.21166667, longitude: 128.00022222), zoomLevel: 8.0),
    ecaInfo(areaId: 53, name: "Korea Busan Port", centerPosition: CLLocationCoordinate2D(latitude: 34.81029311, longitude: 128.89994528), zoomLevel: 8.0),
    ecaInfo(areaId: 54, name: "Korea Busan Port West", centerPosition: CLLocationCoordinate2D(latitude: 34.82819444, longitude: 128.82208333), zoomLevel: 9.0),
    ecaInfo(areaId: 55, name: "Korea Ulsan Port", centerPosition: CLLocationCoordinate2D(latitude: 34.98933675, longitude: 129.52881635), zoomLevel: 8.0),
    ecaInfo(areaId: 61, name: "Panama Canal Atlantic Entrance", centerPosition: CLLocationCoordinate2D(latitude: 7.09898818, longitude: -79.99448871), zoomLevel: 6.0),
    ecaInfo(areaId: 62, name: "Panama Canal Pacific Entrance", centerPosition: CLLocationCoordinate2D(latitude: 6.65073570, longitude: -79.42740275), zoomLevel: 6.0)
]

class EcaArea {
    @ObservedObject var ecaData = SharingData.eca
    var sessionEcaArea = SessionEcaArea()

    private func tableInit() {
        for data in ecaDataTable {
            if !ecaData.ecaArea.keys.contains(data.areaId) {
                var reg = RegisteredEca(id: data.areaId, ecaName: data.name)!
                reg.color = "0xFF0000"          //ライン色（ARGB）
                reg.centerPosition = data.centerPosition
                reg.zoomLevel = data.zoomLevel
                SharingData.eca.ecaArea.updateValue(reg, forKey: data.areaId)
            }
        }
    }

    func start() {
        print(debug: "called")
        tableInit()

        sessionEcaArea.RequestEcaArea(responseEcaArea)
    }

    private func responseEcaArea(result: Result<Data, APIError>) {
        print(debug: "called")
        switch result {
        case .success(let resultData):
            let serverSession = ServerSession()
            let resjson = serverSession.fromJSON(resultData: resultData, resltType: [ResEcaArea].self)
            if let res = resjson {
                print(debug: res)
                for ecaList in res {
                    if let position = ecaList.geometry {
                        var points: [CLLocationCoordinate2D] = []
                        position.forEach { point in
                            var ecaPoint: CLLocationCoordinate2D = CLLocationCoordinate2D()
                            ecaPoint.latitude = CLLocationDegrees(point.lat ?? 0.0)
                            ecaPoint.longitude = CLLocationDegrees(point.lon ?? 0.0)
                            points.append(ecaPoint)
                        }
                        SharingData.eca.ecaArea[ecaList.areaId]?.points = points
                    }
                }
            }
        case .failure(let errorCode):
            print(debug: errorCode)
            break
        }
    }
}
