//
//  TaskExportController.swift
//  NetKnot
//
//  Created by Harry on 2020/11/20.
//  Copyright © 2020 Harry. All rights reserved.
//
//  Mail: miniwing.hz@gmail.com
//  TEL : +85253054612
//

import UIKit

class TaskExportController: IDEAViewController {

   @IBOutlet   weak  var   exportLinkButton        : UIButton!;
   @IBOutlet   weak  var   exportCURLButton        : UIButton!;
   @IBOutlet   weak  var   exportHARButton         : UIButton!;

   @IBOutlet   weak  var   cancelButton            : UIButton!;

   @IBOutlet   weak  var   contentView             : UIView!;
   @IBOutlet   weak  var   bottomView              : UIView!;

   var                     exportLinkBlock         : ((_ aViewController : UIViewController) -> Void)?   = nil;
   var                     exportCURLBlock         : ((_ aViewController : UIViewController) -> Void)?   = nil;
   var                     exportHARBlock          : ((_ aViewController : UIViewController) -> Void)?   = nil;

   var                     cancelBlock             : ((_ aViewController : UIViewController) -> Void)?   = nil;

   override func viewDidLoad() {

      super.viewDidLoad();

      // Do any additional setup after loading the view.
      
      self.view.backgroundColor        = UIColor.clear;
      
      self.bottomView.backgroundColor  = UIColor.clear;
      
      self.contentView.backgroundColorPicker = DKColorTable.shared()?.picker(withKey: APPColor.systemBackground());
//      self.contentView.setCornerRadius(8);
      
      self.contentView.setCornerRadius(8, clipsToBounds: true);
      
      /**
       LINK
       */
      self.exportLinkButton.backgroundColorPicker  = DKColorTable.shared()?.picker(withKey: APPColor.systemBackground());
      self.exportLinkButton.setTitleColorPicker(DKColorTable.shared()?.picker(withKey: APPColor.label()), for: UIControl.State.normal);
      
      self.exportLinkButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.secondarySystemBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.normal);

      self.exportLinkButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.systemGroupedBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.selected);
      
      /**
       CURL
       */
      self.exportCURLButton.backgroundColorPicker  = DKColorTable.shared()?.picker(withKey: APPColor.systemBackground());
      self.exportCURLButton.setTitleColorPicker(DKColorTable.shared()?.picker(withKey: APPColor.label()), for: UIControl.State.normal);

      self.exportCURLButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.secondarySystemBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.normal);

      self.exportCURLButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.systemGroupedBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.selected);

      /**
       HAR
       */
      self.exportHARButton.backgroundColorPicker   = DKColorTable.shared()?.picker(withKey: APPColor.systemBackground());
      self.exportHARButton.setTitleColorPicker(DKColorTable.shared()?.picker(withKey: APPColor.label()), for: UIControl.State.normal);
      
      self.exportHARButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.secondarySystemBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.normal);

      self.exportHARButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.systemGroupedBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.selected);

      /**
       Cancel Button
       */
      self.cancelButton.setCornerRadius(8, clipsToBounds: true);
      
      self.cancelButton.backgroundColorPicker   = DKColorTable.shared()?.picker(withKey: APPColor.systemBackground());
      self.cancelButton.setTitleColorPicker(DKColorTable.shared()?.picker(withKey: APPColor.label()), for: UIControl.State.normal);

      self.cancelButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.secondarySystemBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.normal);

      self.cancelButton.setBackgroundImagePicker({ (themeVersion) -> UIImage? in
         let   stPicker : DKColorPicker   = (DKColorTable.shared()?.picker(withKey: APPColor.systemGroupedBackground()))!;
         if DKThemeVersionNight == themeVersion {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End if () */
         else {
            return UIImage(color: stPicker(themeVersion)!);
         } /* End else */
      }, for: UIControl.State.selected);

      self.exportLinkButton.setTitle("Export link".localized, for: UIControl.State.normal);
      self.exportCURLButton.setTitle("Export cURL".localized, for: UIControl.State.normal);
      self.exportHARButton.setTitle("Export HTTP Archive (.har)".localized, for: UIControl.State.normal);
      self.cancelButton.setTitle("Cancel".localized, for: UIControl.State.normal);

      return;
   }

   override func didReceiveMemoryWarning() {

      super.didReceiveMemoryWarning();
      // Dispose of any resources that can be recreated.

      return;
   }

   override func viewWillAppear(_ animated: Bool) {

      super.viewWillAppear(animated);

      return;
   }

   override func viewDidAppear(_ animated: Bool) {

      super.viewDidAppear(animated);

      return;
   }

   override func viewWillDisappear(_ animated: Bool) {

      super.viewWillDisappear(animated);

      return;
   }

   override func viewDidDisappear(_ animated: Bool) {

      super.viewDidDisappear(animated);

      return;
   }
}

// MARK: - Navigation
extension TaskExportController {
   
   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
      
      return;
   }
}

// MARK: - UIStatusBarStyle
extension TaskExportController {

   /// 状态栏样式
   override var preferredStatusBarStyle : UIStatusBarStyle {

      if DKNightVersionManager.themeManager()?.themeVersion == DKThemeVersionNight {
         
         return UIStatusBarStyle.lightContent;

      } /* End if () */
      else {

         if #available(iOS 13, *) {
            
            return UIStatusBarStyle.darkContent;

         } /* End if () */

         return UIStatusBarStyle.default;

      } /* End else */

   }

   /// 状态栏是否隐藏
   override var prefersStatusBarHidden: Bool {

      return false;
   }

   /// 状态栏的隐藏与显示动画样式
   override var preferredStatusBarUpdateAnimation : UIStatusBarAnimation {

      return UIStatusBarAnimation.fade;
   }

   override var supportedInterfaceOrientations : UIInterfaceOrientationMask {

      return UIInterfaceOrientationMask.portrait;
   }

   override var shouldAutorotate : Bool {

      return false;
   }

   override var prefersHomeIndicatorAutoHidden : Bool {

      return true;
   }

   // MARK: - 模态样式
//   open override var modalPresentationStyle : UIModalPresentationStyle {
//   
//      get {
//         return UIModalPresentationStyle.fullScreen;
//      }
//      
//      set {
//         super.modalPresentationStyle  = newValue;
//      }
//   }
}

// MARK: - IDEAPresentationControllerDelegate
// extension TaskExportController : IDEAPresentationControllerDelegate
extension TaskExportController {
   
   @objc override func frameOfPresented() -> CGRect {
      return CGRect(x: 0,
                    y: self.view.height - (self.bottomView.height + (UIApplication.shared.keyWindow?.safeArea().bottom)!),
                    width: self.view.width,
                    height: (self.bottomView.height + (UIApplication.shared.keyWindow?.safeArea().bottom)!));
   }
}

// MARK: - Block
extension TaskExportController {

   func setExportLinkBlock(_ aExportLinkBlock: ((_ aViewController : UIViewController) -> Void)!) -> Void {
    
      self.exportLinkBlock = aExportLinkBlock;
      
      return;
   }

   func setExportCURLBlock(_ aExportCURLBlock: ((_ aViewController : UIViewController) -> Void)!) -> Void {
    
      self.exportCURLBlock = aExportCURLBlock;
      
      return;
   }

   func setExportHARBlock(_ aExportHARBlock: ((_ aViewController : UIViewController) -> Void)!) -> Void {
    
      self.exportHARBlock  = aExportHARBlock;
      
      return;
   }

   func setCancelBlock(_ aCancelBlock: ((_ aViewController : UIViewController) -> Void)!) -> Void {
    
      self.cancelBlock     = aCancelBlock;
      
      return;
   }
}

// MARK: - IBAction
extension TaskExportController {

   @IBAction func onExportLink(_ aSender: UIButton) -> Void {
      
      if TunnelService.DEBUG { TunnelService.Debug("TaskExportController::onExportLink() : Sender : \(aSender)"); }

      if nil != self.exportLinkBlock {
         
         self.exportLinkBlock!(self);
         
      } /* End if () */
      
      return;
   }

   @IBAction func onExportCURL(_ aSender: UIButton) -> Void {
      
      if TunnelService.DEBUG { TunnelService.Debug("TaskExportController::onExportCURL() : Sender : \(aSender)"); }

      if nil != self.exportCURLBlock {
         
         self.exportCURLBlock!(self);
         
      } /* End if () */

      return;
   }

   @IBAction func onExportHAR(_ aSender: UIButton) -> Void {
      
      if TunnelService.DEBUG { TunnelService.Debug("TaskExportController::onExportHAR() : Sender : \(aSender)"); }

      if nil != self.exportHARBlock {
         
         self.exportHARBlock!(self);
         
      } /* End if () */
      
      return;
   }

   @IBAction func onCancel(_ aSender: UIButton) -> Void {
      
      if TunnelService.DEBUG { TunnelService.Debug("TaskExportController::onCancel() : Sender : \(aSender)"); }
      
      if nil != self.cancelBlock {
         
         self.cancelBlock!(self);
         
      } /* End if () */

      return;
   }
}


// MARK: - UITheme
extension TaskExportController {
   
   @objc override func onThemeUpdate(_ aNotification : Notification) -> Void  {

      if TunnelService.DEBUG {TunnelService.Debug("TaskExportController::onThemeUpdate(notification:)");}

      if super.responds(to: #selector(onThemeUpdate(_:))) {
         
         super.onThemeUpdate(aNotification);
         
      } /* End if () */

      var stTitleAttributes : Dictionary<NSAttributedString.Key, Any>;

      if nil == self.navigationController?.navigationBar.titleTextAttributes {
         
         stTitleAttributes = NSMutableDictionary() as! [NSAttributedString.Key : Any];
                  
      } /* End if () */
      else {
         
         stTitleAttributes = (self.navigationController?.navigationBar.titleTextAttributes)!;

      } /* End else */

      stTitleAttributes.updateValue(IDEAColor.color(withKey: IDEAColor.label()), forKey: NSAttributedString.Key.foregroundColor);
      
      self.navigationController?.navigationBar.titleTextAttributes   = stTitleAttributes;

      if DKThemeVersionNight == DKNightVersionManager.shared().themeVersion {
         
      } /* End if () */
      else {
         
      } /* End else */

      return;
   }
}
