//
//  AppDelegate.swift
//  JSONCreator
//
//  Created by Scott Pfeil on 8/2/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit
import MVMCoreUI
import VDS

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
    
    var window: UIWindow?
    var mvcNav: UINavigationController?
    var dvcNav: UINavigationController?
    var mvc: UIViewController?
    var dvc: UIViewController?
    
    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        // Setup our core object with the default implementation
        CoreUIObject.sharedInstance()?.defaultInitialSetup()
        CoreUIObject.sharedInstance()?.globalTopAlertDelegate = self
        
        //        MVMCoreCache.shared()?.addPage(toCache: [KeyPageType:"videotest","template":"stack","header":[KeyMoleculeName:"header","molecule":["moleculeName":"twoButtonView","primaryButton":["moleculeName":"button","title":"back","action":[KeyActionType:KeyActionTypeBack]],"secondaryButton":["moleculeName":"button","title":"open","action":[KeyActionType:KeyActionTypeOpen,KeyPageType:"planSelectionPageType"]]]],"stack":[KeyMoleculeName:"stack","molecules":[]]], pageType: "videotest")
        //
        //        let path = Bundle.main.path(forResource: "Testing3", ofType: "json")!
        //        let string = try! String(contentsOfFile: path, encoding: .utf8)
        //        let dict: [AnyHashable: Any] = try! JSONSerialization.jsonObject(with: string.data(using: .utf8)!, options: .fragmentsAllowed) as! [AnyHashable: Any]
        //        let page: [AnyHashable: Any] = dict[KeyPage] as! [AnyHashable: Any]
        //        MVMCoreCache.shared()?.addPage(toCache: page, pageType: page[KeyPageType]! as! String)
        return true
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // Override point for customization after application launch.
        let splitViewController = window!.rootViewController as! UISplitViewController
        mvcNav = splitViewController.viewControllers[0] as? UINavigationController
        mvcNav?.delegate = self
        mvc = mvcNav?.topViewController
        dvcNav = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as? UINavigationController
        dvc = dvcNav?.topViewController
        dvc?.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
        dvc?.navigationItem.leftItemsSupplementBackButton = true
        splitViewController.delegate = self
        splitViewController.preferredDisplayMode = .allVisible
        
        let gr = UILongPressGestureRecognizer(target: self, action: #selector(jsonPasteAndGo))
        splitViewController.view.addGestureRecognizer(gr)
        
        NotificationCenter.default.addObserver(forName: UIViewController.showDetailTargetDidChangeNotification, object: splitViewController, queue: nil) { [weak self] (notification) in
            if let strongSelf = self, let svc = notification.object as? UISplitViewController {
                if svc.isCollapsed {
                    strongSelf.mvc?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Editor", style: .plain, target: self, action: #selector(self?.showEditor))
                } else {
                    strongSelf.mvcNav?.setViewControllers([strongSelf.mvc!], animated: false)
                    strongSelf.mvc?.navigationItem.rightBarButtonItem = nil
                }
            }
        }
        self.register()
        return true
    }
    
    @objc func jsonPasteAndGo(_ sender: UILongPressGestureRecognizer) {
        if sender.state == .ended {
            (dvc as? DetailViewController)?.textView.text = UIPasteboard.general.string
            (dvc as? DetailViewController)?.play()
        }
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    // MARK: - Split view
    
    func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
        //guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false }
        //guard let _ = secondaryAsNavController.topViewController as? DetailViewController else { return false }
        return true
    }
    
    @objc func showEditor() {
        mvcNav?.pushViewController(dvc!, animated: true)
    }
}

extension AppDelegate: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if mvc == viewController {
            dvcNav?.setViewControllers([dvc!], animated: false)
        }
    }
}

extension AppDelegate: MVMCoreGlobalTopAlertDelegateProtocol {
    func getTopAlertView() -> UIView & MVMCoreTopAlertViewProtocol {
        guard let topAlertView = MVMCoreUISplitViewController.main()?.topAlertView else {
            return MVMCoreUITopAlertView()
        }
        return topAlertView;
    }
    
    func priorityForTopAlert(by object: MVMCoreTopAlertObject) -> Operation.QueuePriority {
        return object.queuePriority
    }
}

extension AppDelegate {
    func register(){
        ModelRegistry.register(handler: TestLabelToggle.self, for: TestLabelToggleModel.self)
        ModelRegistry.register(handler: TestToggle.self, for: TestToggleModel.self)
        ModelRegistry.register(handler: TextEntryField.self, for: TextEntryField64Model.self)
        ModelRegistry.register(handler: EmailVerifyField.self, for: EmailVerifyModel.self)
        ModelRegistry.register(handler: WifiWidget.self, for: WifiWidgetModel.self)
        ModelRegistry.register(handler: ToggleWifiActionHandler.self, for: ToggleWifiActionModel.self)
    }
}

@objcMembers open class EmailVerifyModel: MoleculeModelProtocol {

    public static var identifier: String = "emailVerifyField"
    public var moleculeName: String = EmailVerifyModel.identifier
    public var emailField: TextEntryFieldModel
    public var bottomMolecule: MoleculeModelProtocol
    public var email: String
    public var backgroundColor: MVMCoreUI.Color?
    
    public init(emailField: TextEntryFieldModel, email: String, bottomMolecule: MoleculeModelProtocol) {
        self.emailField = emailField
        self.email = email
        self.bottomMolecule = bottomMolecule
    }
    
    public required init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        emailField = try typeContainer.decode(TextEntryFieldModel.self, forKey: .emailField)
        email = try typeContainer.decode(String.self, forKey: .email)
        bottomMolecule = try typeContainer.decodeModel(codingKey: .bottomMolecule)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(emailField, forKey: .emailField)
        try container.encode(email, forKey: .email)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeModel(bottomMolecule, forKey: .bottomMolecule)
    }
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case emailField
        case bottomMolecule
        case backgroundColor
        case email
    }
}

@objcMembers open class EmailVerifyField: View {
   
    private let emailField = TextEntryField()
    
    private var bottomView: MoleculeViewProtocol?
    
    private var emailVerifyFieldModel: EmailVerifyModel? {
        return model as? EmailVerifyModel
    }
    
    private var isBottomViewHidden: Bool {
        guard let emailVerifyFieldModel = emailVerifyFieldModel, let text = emailField.text else { return true }
        return !(emailVerifyFieldModel.email.caseInsensitiveCompare(text) == .orderedSame)
    }
    
    private let containerView = MVMCoreUICommonViewsUtility.commonView()
    
    open override func setupView() {
       super.setupView()
       backgroundColor = .clear
       clipsToBounds = true
       addSubview(containerView)
       NSLayoutConstraint.constraintPinSubview(toSuperview: containerView)
       containerView.addSubview(emailField)
       NSLayoutConstraint.constraintPinSubview(emailField, pinTop: true, pinBottom: false, pinLeft: true, pinRight: true)
       addEmailFieldObserver()
    }
    
    private func addEmailFieldObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(onEmailValueChange), name: UITextField.textDidChangeNotification, object: emailField.textField)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? EmailVerifyModel else { return }
        
        emailField.set(with: model.emailField, delegateObject, additionalData)
                
        if let bottomView = self.bottomView ?? ModelRegistry.createMolecule(model.bottomMolecule, delegateObject: delegateObject, additionalData: additionalData) {
            bottomView.set(with: model.bottomMolecule, delegateObject, additionalData)
          self.bottomView = bottomView
          addBottomView(bottomView: bottomView)
        }
    }
    
    public func onEmailValueChange() {
        bottomView?.isHidden = isBottomViewHidden
    }
    
    private func addBottomView(bottomView: MoleculeViewProtocol) {
        
        containerView.addSubview(bottomView)
        
        bottomView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
        containerView.trailingAnchor.constraint(equalTo: bottomView.trailingAnchor).isActive = true
        bottomView.topAnchor.constraint(equalTo: emailField.bottomAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: bottomView.bottomAnchor).isActive = true
        bottomView.isHidden = isBottomViewHidden
    }
}

class ToggleWifiActionModel: ActionModelProtocol {
    static var identifier: String = "toggleWifi"
    var extraParameters: JSONValueDictionary?
    var analyticsData: JSONValueDictionary?
    var wifiId: String
    var actionType: String = ToggleWifiActionModel.identifier
    
    init(wifiId: String) {
        self.wifiId = wifiId
    }
}

class ToggleWifiActionHandler: MVMCoreActionHandlerProtocol {
    required public init() {}

    func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable : Any]?, delegateObject: DelegateObject?) {
        guard let action = model as? ToggleWifiActionModel else { return }
        
        print("Wi-Fi Id: \(action.wifiId)")
        
    }
}

class TextEntryField64Model: TextEntryFieldModel {
    open override class var identifier: String { "textFieldBase64" }
    
    open override func formFieldServerValue() -> AnyHashable? {
        guard let value = super.formFieldServerValue() as? String else { return nil }
        return value.base64Encoded()
    }
    
    required init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        text = text?.base64Decoded()
    }
}

extension String {

    func base64Encoded() -> String? {
        data(using: .utf8)?.base64EncodedString()
    }

    func base64Decoded() -> String? {
        guard let data = Data(base64Encoded: self) else { return nil }
        return String(data: data, encoding: .utf8)
    }
}
