//
//  ListWorkViewController.swift
//  Em-call
//
//  Created by Alex Sh on 14.03.2022.
//

import UIKit
import CoreGraphics

protocol ListWorkViewControllerProtocol:class {
    
    /// Called when one of the supported character categories did change
    /// - Parameters:
    ///   - race: the acutal race character
    ///   - weapon: the actual weapon character
    ///   - coa: the actual coat of arms character
    func charactersDidChange(race:Character, weapon:Character, coa:Character)
}

class ListWorkViewController: UIViewController {
   // var settingsLauncher:SettingsLauncher = SettingsLauncher()
    var debug = 0
    /// Any delegate for this view
    weak var delegate: ListWorkViewControllerProtocol?
    let fullView: CGFloat = 100
    var partialView: CGFloat {
      // return UIScreen.main.bounds.height - (400 + UIApplication.shared.statusBarFrame.height)
        return UIScreen.main.bounds.height - 200
    }
   // var partialView: CGFloat {        return UIScreen.main.bounds.height - 400    }
    override func viewDidLoad() {
        super.viewDidLoad()
      //  let gesture = UIPanGestureRecognizer.init(target: self, action: #selector(ListWorkViewController.panGesture))
  
        //gesture.delegate = self
       // view.addGestureRecognizer(gesture)
        roundViews()
    }

    @IBAction func pressBottomPanel(_ sender: Any) {
        callElectric()
    }
    
    func callElectric()
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
         let vc:UIViewController
         if #available(iOS 13.0, *) {
              vc = storyboard.instantiateViewController(identifier: "Double_ElectricVC") as! DoubleElectricVC
         } else {
             vc = storyboard.instantiateViewController(withIdentifier: "Double_ElectricVC")
         }
        self.navigationController?.push(viewController: vc)
    }
    
    @IBAction func pressWorkType_1(_ sender: Any) {
        callElectric()
    }
    
    @IBAction func pressWorkType_4(_ sender: Any) {
        callElectric()
    }
    
    @IBAction func pressWorkType_5(_ sender: Any) {
        callElectric()
    }
    @IBAction func pressWorkType_6(_ sender: Any) {
        callElectric()
    }
    @IBAction func pressWorkType_7(_ sender: Any) {
        callElectric()
    }
    @IBAction func pressWorkType_8(_ sender: Any) {
        callElectric()
    }
    @IBAction func pressWorkType_9(_ sender: Any) {
        callElectric()
    }
    
    @IBAction func pressWorkType_2(_ sender: Any) {
        callElectric()
    }
    @IBAction func pressWorkType_3(_ sender: Any) {
        callElectric()
    }
    
    func roundViews() {
        view.layer.cornerRadius = 5
       /* holdView.layer.cornerRadius = 3
        left.layer.cornerRadius = 10
        right.layer.cornerRadius = 10
      //  left.layer.borderColor = UIColor(colorLiteralRed: 0, green: 148/255, blue: 247.0/255.0, alpha: 1).cgColor
        left.layer.borderWidth = 1*/
        view.clipsToBounds = true
    }
    

    override func viewDidAppear(_ animated: Bool) {
  /*
        UIView.animate(withDuration: 0.6, animations: { [weak self] in
            let frame = self?.view.frame
            let yComponent = self?.partialView
            self?.view.frame = CGRect(x: 0, y: yComponent!, width: frame!.width, height: frame!.height - 100)
            })*/
    }
    
    //MARK: Private
    
     func setup(){
        //self.view.backgroundColor = UIColor.Showcase8.colorB2
        //self.loadData()
    }
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
      //  prepareBackgroundView()
    }
}

extension ListWorkViewController: UIGestureRecognizerDelegate {

    // Solution
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        let gesture = (gestureRecognizer as! UIPanGestureRecognizer)
        let direction = gesture.velocity(in: view).y

        let y = view.frame.minY
       // if (y == fullView && tableView.contentOffset.y == 0 && direction > 0) || (y == partialView) {
         if (y == fullView  && direction > 0) || (y == partialView) {
           // tableView.isScrollEnabled = false
        } else {
           // tableView.isScrollEnabled = true
        }
        
        return false
    }
}


/*
 @objc func panGesture(_ recognizer: UIPanGestureRecognizer) {
     let translation = recognizer.translation(in: self.view)
     let velocity = recognizer.velocity(in: self.view)

     let y = self.view.frame.minY
     if (y + translation.y >= fullView) && (y + translation.y <= partialView) {
         self.view.frame = CGRect(x: 0, y: y + translation.y, width: view.frame.width, height: view.frame.height)
         recognizer.setTranslation(CGPoint.zero, in: self.view)
     }
     
     if recognizer.state == .ended {
         var duration =  velocity.y < 0 ? Double((y - fullView) / -velocity.y) : Double((partialView - y) / velocity.y )
         
         duration = duration > 1.3 ? 1 : duration
         
         UIView.animate(withDuration: duration, delay: 0.0, options: [.allowUserInteraction], animations: {
             if  velocity.y >= 0 {
                 self.view.frame = CGRect(x: 0, y: self.partialView, width: self.view.frame.width, height: self.view.frame.height)
             } else {
                 self.view.frame = CGRect(x: 0, y: self.fullView, width: self.view.frame.width, height: self.view.frame.height)
             }
             
             }, completion: { [weak self] _ in
                 if ( velocity.y < 0 ) {
                     self?.debug = 1
                 }
         })
     }
 }
 
 func prepareBackgroundView(){
     let blurEffect = UIBlurEffect.init(style: .dark)
     let visualEffect = UIVisualEffectView.init(effect: blurEffect)
     let bluredView = UIVisualEffectView.init(effect: blurEffect)
     bluredView.contentView.addSubview(visualEffect)
     
     visualEffect.frame = UIScreen.main.bounds
     bluredView.frame = UIScreen.main.bounds
     
     view.insertSubview(bluredView, at: 0)
 }
*/
/*
func panGesture(recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: self.view)
    let y = self.view.frame.minY
    self.view.frame = CGRect(x: 0, y: y + translation.y, width: view.frame.width, height: view.frame.height)
   // recognizer.setTranslation(CoreGraphics.CGPointZero, in: self.view)
    recognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
   // CGRect(x: 0, y: y + translation.y, width: view.frame.width, height: view.frame.height)
}
*/
