//
//  MainAppViewController.swift
//  Runner
//
//  Created by Zohaib Iqbal Kambrani on 08/06/2021.
//  Copyright © 2021 The Chromium Authors. All rights reserved.
//

import Foundation
import AADraggableView

class MainAppViewController: FlutterViewController{
    var videoCallContainer:AADraggableView!
    var videoCallViewController:VideoCallViewController!
    var videoCallFlutterResult:FlutterResult?
    var vdoCallViewFloatRectConstraint:[NSLayoutConstraint]!
    var vdoCallViewFullConstraint:[NSLayoutConstraint]!
    var vdoCallViewFloatCircleConstraint:[NSLayoutConstraint]!

    
    override func viewDidLoad() {
        super.viewDidLoad()
        initFlutterBridge()
        prepareVideoCallView()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    var videoCallChannel:FlutterMethodChannel?
    private func initFlutterBridge(){
        videoCallChannel = FlutterMethodChannel(name: "Dr.cloudSolution/videoCall", binaryMessenger: binaryMessenger)
        videoCallChannel?.setMethodCallHandler({
            (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
            switch call.method {
            case "openVideoCall":
              self.startVideoCall(result: result, call: call)
            default:
                result(FlutterMethodNotImplemented)
              }
        })
    }
    
}


// Video Call Functions
extension MainAppViewController : ICallProtocol{
    
    func prepareVideoCallView(){
        videoCallContainer = AADraggableView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
        videoCallContainer.backgroundColor = UIColor.black
        videoCallContainer.isHidden = true
        videoCallContainer.clipsToBounds = true
        videoCallContainer.padding = 20
        videoCallContainer.reposition = .edgesOnly
        videoCallContainer.isEnabled = false
        
        view.addSubview(videoCallContainer)
        setVideoViewConstrints()
        NSLayoutConstraint.activate(vdoCallViewFullConstraint)
        NSLayoutConstraint.deactivate(vdoCallViewFloatRectConstraint)        
        
        ViewEmbedder.embed(
            withIdentifier: "videoCall", // Storyboard ID
            parent: self,
            container: self.videoCallContainer){ vc in
            self.videoCallViewController = vc as? VideoCallViewController

        }
    }
    
    private func showVideo(show:Bool){
        self.videoCallContainer.hidden(!show, rootView: view)
    }
    
    private func startVideoCall(result: @escaping FlutterResult, call:FlutterMethodCall) {
        videoCallFlutterResult = result
        
        if let arguments = call.arguments as? NSDictionary{
            showVideoCallView(true)
            
            videoCallViewController.onRectFloat = { isRectFloat in
                self.rectFloatVideoCallView(isRectFloat)
            }
            
            videoCallViewController.onCircleFloat = { isCircleFloat in
                self.circleFloatVideoCallView(isCircleFloat)
            }
            videoCallViewController.onCallConnect = {
                self.videoCallChannel?.invokeMethod("onCallConnected",arguments: nil)
            }
            videoCallViewController.onCallDisconnect = {
                self.showVideoCallView(false)
                self.videoCallViewController.minimizeVideoState(state: false)
                self.videoCallChannel?.invokeMethod("onCallDisconnected",arguments: nil)
            }
            videoCallViewController.callBack = self
            videoCallViewController.start(params: VideoCallRequestParameters(dictionary: arguments))
        }
        
          
    }
    
    private func rectFloatVideoCallView(_ value:Bool){
        videoCallContainer.enable(value)
        
        UIView.animate(withDuration: 0.5) {
            if(value){
                NSLayoutConstraint.deactivate(self.vdoCallViewFullConstraint)
                NSLayoutConstraint.deactivate(self.vdoCallViewFloatCircleConstraint)
                NSLayoutConstraint.activate(self.vdoCallViewFloatRectConstraint)
            }else{
                NSLayoutConstraint.deactivate(self.vdoCallViewFloatRectConstraint)
                NSLayoutConstraint.deactivate(self.vdoCallViewFloatCircleConstraint)
                NSLayoutConstraint.activate(self.vdoCallViewFullConstraint)
            }
            self.videoCallContainer.layer.cornerRadius = value ? 10 : 0
            self.videoCallContainer.layer.borderColor = value ? UIColor.white.cgColor : nil
            self.videoCallContainer.layer.borderWidth = value ? 2 : 0
            self.view.layoutIfNeeded()
        }
    }
    
    private func circleFloatVideoCallView(_ value:Bool){
        videoCallContainer.reposition = value ? .sticky : .edgesOnly
        
        UIView.animate(withDuration: 0.5) {
            if(value){
                NSLayoutConstraint.deactivate(self.vdoCallViewFullConstraint)
                NSLayoutConstraint.deactivate(self.vdoCallViewFloatRectConstraint)
                NSLayoutConstraint.activate(self.vdoCallViewFloatCircleConstraint)
                self.videoCallContainer.layer.cornerRadius = 35
            }else{
                NSLayoutConstraint.activate(self.vdoCallViewFloatRectConstraint)
                NSLayoutConstraint.deactivate(self.vdoCallViewFullConstraint)
                NSLayoutConstraint.deactivate(self.vdoCallViewFloatCircleConstraint)
                self.videoCallContainer.layer.cornerRadius = 10

            }
            self.videoCallContainer.layer.borderColor = value ? UIColor.white.cgColor : nil
            self.videoCallContainer.layer.borderWidth = value ? 2 : 0
            self.view.layoutIfNeeded()
        }
    }
    
    private func showVideoCallView(_ value:Bool){
        self.videoCallContainer.hidden(!value, rootView: view)
    }
    
    func sessionDone(res: Any) {
        videoCallFlutterResult?(res)
    }
    
    func sessionNotResponded(res: Any) {
        videoCallFlutterResult?(res)
    }
    
    
    func setVideoViewConstrints(){
        videoCallContainer.layer.shadowColor = UIColor.black.cgColor
        videoCallContainer.layer.shadowOffset = CGSize(width: 1, height: 1)
        videoCallContainer.layer.shadowRadius = 5
        
        let screen = UIScreen.main.bounds
        
        videoCallContainer.translatesAutoresizingMaskIntoConstraints = false
        
        vdoCallViewFullConstraint = [
            videoCallContainer.topAnchor.constraint(equalTo: view.topAnchor),
            videoCallContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            videoCallContainer.widthAnchor.constraint(equalToConstant: screen.width),
            videoCallContainer.heightAnchor.constraint(equalToConstant: screen.height)
        ]
        vdoCallViewFloatRectConstraint = [
            videoCallContainer.topAnchor.constraint(equalTo: view.topAnchor, constant: 40),
            videoCallContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            videoCallContainer.widthAnchor.constraint(equalToConstant: screen.width/3),
            videoCallContainer.heightAnchor.constraint(equalToConstant: screen.height/3.5)
        ]
        vdoCallViewFloatCircleConstraint = [
            videoCallContainer.topAnchor.constraint(equalTo: view.topAnchor, constant: 40),
            videoCallContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            videoCallContainer.widthAnchor.constraint(equalToConstant: 70),
            videoCallContainer.heightAnchor.constraint(equalToConstant: 70)
        ]
    }
    
}
