//
//  Initial.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/4.
//

import Foundation
import DDMAMapKit_Private
import IQKeyboardManagerSwift
import DDAutoUIKit_Private
import DDNetworkingOfAlamofireKit_Private
import Alamofire
import DDToastKit_Private
import DDCategoryKit_Private
import DDControlsKit_Private

class Initial : NSObject {
    static let shareInstance = Initial.init()

    // MARK: <Third>
    func initThirdLib() {
        // 键盘
        initIQKeyboard()

        // mamap
        initMaMapKey()

        // autoUI
        initAutoUI()
        
        // af
        initAF()
    }
    
    func initIQKeyboard(){
        IQKeyboardManager.shared.enable = true
    }
    
    func initMaMapKey(){
        DDMAMapView.start(withAppKey: MAAppKey)
        DDMAMapView.agreePrivacy()
    }
    
    func initAutoUI() {
        let _ = AutoUI.default.getScale(size: 375)
    }
    
    func initAF() {
        DDAF.delegate = self
    }
    
    // MARK: <RootViewController>
    func initRootViewController(){
        if USER.token != nil {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: Notification_loginSuccessToRescueController), object: nil)
        }else{
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: Notification_toLoginController), object: nil)
        }
    }
    
    func changeRootViewController(window:UIWindow?) {

        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: Notification_toLoginController), object: nil, queue: .main) { note in
            let nav = DDNavigationController(rootViewController: LoginController.init())
            window?.rootViewController = nav
        }
        
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: Notification_logoutSuccessToLoginController), object: nil, queue: .main) { note in
            let nav = DDNavigationController(rootViewController: LoginController.init())
            window?.rootViewController = nav
        }
        
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: Notification_reLoginToLoginController), object: nil, queue: .main) { note in
            let nav = DDNavigationController(rootViewController: LoginController.init())
            window?.rootViewController = nav
        }
        
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: Notification_loginSuccessToRescueController), object: nil, queue: .main) { note in
            window?.rootViewController = MainTabBarController.init()
        }
        
    }
}

extension Initial : DDAlamofireDelegate {
    func networkErrorHandler<T>(response: AFDataResponse<T>) -> AFDataResponse<T> where T : Decodable {
        DispatchQueue.main.async {
            UIApplication.shared.dd_keyWindow.dd_makeToast(networkFailed)
        }
        return response
    }
    
    func errorCodeHandler<T>(response: AFDataResponse<T>) -> AFDataResponse<T> where T : Decodable {
        if response.response?.statusCode == 401 {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: Notification_logoutSuccessToLoginController), object: nil)
        }
        
        if let error = response.error {
            DispatchQueue.main.async {
                UIApplication.shared.dd_keyWindow.dd_makeToast(error.errorDescription)
            }
        }
        
        if let data = response.data {
            let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
            let dic = json as? Dictionary<String, Any>
            let msg = dic?["msg"] as? String
            if let msg {
                DispatchQueue.main.async {
                    UIApplication.shared.dd_keyWindow.dd_makeToast(msg)
                }
            }
        }
        
        return response
    }
    
    func completionHandler<T>(response: AFDataResponse<T>) -> AFDataResponse<T> where T : Decodable {
        return response
    }
}
