//
//  MonitoringRoute.swift
//  Sailassist
//
//  Created by 三浦薫巳 on 2023/11/14.
//

import Foundation
import CoreLocation

class MonitoringRoute {
    var sessionMonitoringRoute = SessionMonitoringRoute()

    func start() {
        print(debug: "called")
        sessionMonitoringRoute.RequestMonitoringRoute(responseMonitoringRoute)
    }

    private func responseMonitoringRoute(result: Result<Data, APIError>) {
        print(debug: "called")
        switch result {
        case .success(let resultData):
            let serverSession = ServerSession()
            let resjson = serverSession.fromJSON(resultData: resultData, resltType: ResMonitoringRoute.self)
            if let res = resjson {
                SharingData.map.wayPoints = []
                if let wayPoint = res.wayPoints {
                    wayPoint.forEach { waypoint in
                        var point: CLLocationCoordinate2D = CLLocationCoordinate2D()
                        point.latitude = CLLocationDegrees(waypoint.lat ?? 0.0)
                        point.longitude = CLLocationDegrees(waypoint.lon ?? 0.0)
                        SharingData.map.wayPoints.append(point)
                    }
                }

                SharingData.map.legLine = []
                if let legLine = res.legLine {
                    legLine.forEach { legline in
                        var point: CLLocationCoordinate2D = CLLocationCoordinate2D()
                        point.latitude = CLLocationDegrees(legline.lat ?? 0.0)
                        point.longitude = CLLocationDegrees(legline.lon ?? 0.0)
                        SharingData.map.legLine.append(point)
                    }
                    SharingData.map.legLine = MapViewController.checkStraddleMeridian(SharingData.map.legLine)
                }

                SharingData.map.portLine = []
                if let portLine = res.portLine {
                    portLine.forEach { portline in
                        var point: CLLocationCoordinate2D = CLLocationCoordinate2D()
                        point.latitude = CLLocationDegrees(portline.lat ?? 0.0)
                        point.longitude = CLLocationDegrees(portline.lon ?? 0.0)
                        SharingData.map.portLine.append(point)
                    }
                    SharingData.map.portLine = MapViewController.checkStraddleMeridian(SharingData.map.portLine)
                }

                SharingData.map.starboardLine = []
                if let starboardLine = res.starboardLine {
                    starboardLine.forEach { starboardline in
                        var point: CLLocationCoordinate2D = CLLocationCoordinate2D()
                        point.latitude = CLLocationDegrees(starboardline.lat ?? 0.0)
                        point.longitude = CLLocationDegrees(starboardline.lon ?? 0.0)
                        SharingData.map.starboardLine.append(point)
                    }
                    SharingData.map.starboardLine = MapViewController.checkStraddleMeridian(SharingData.map.starboardLine)
                }
            }
        case .failure(let errorCode):
            print(debug: "serverError: \(errorCode)")
            print(debug: errorCode)
            break
        }
    }
}
