//
//  SignatureWriterViewController.swift
//  iPOS
//
//  Created by BBC on 06/04/17.
//  Copyright © 2017 BBC. All rights reserved.
//

import Foundation

protocol SignatureDelegate{
    func getSignatureDelegate(image: UIImage)
}

class SignatureWriterViewController: UIViewController {
    var lastPoint:CGPoint!
    var isSwiping:Bool!
    var delegate: SignatureDelegate!
    @IBOutlet var imageView: UIImageView!
    
    @IBAction func saveImage(_ sender: AnyObject) {
        if let image = self.imageView.image{
            self.delegate.getSignatureDelegate(image: image)
        }
        return
    }
    
    @IBAction func undoDrawing(_ sender: AnyObject) {
        self.imageView.image = nil
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeRight
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: Touch events
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
        isSwiping = false
        if let touch = touches.first{
            lastPoint = touch.location(in: imageView)
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
        isSwiping = true;
        if let touch = touches.first{
            let currentPoint = touch.location(in: imageView)
            UIGraphicsBeginImageContext(self.imageView.frame.size)
            self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height))
            UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
            UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))
            UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
            UIGraphicsGetCurrentContext()?.setLineWidth(5.0)
            UIGraphicsGetCurrentContext()?.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1.0)
            UIGraphicsGetCurrentContext()?.strokePath()
            self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            lastPoint = currentPoint
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
        if(!isSwiping) {
            // This is a single touch, draw a point
            UIGraphicsBeginImageContext(self.imageView.frame.size)
            self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height))
            UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
            UIGraphicsGetCurrentContext()?.setLineWidth(5.0)
            UIGraphicsGetCurrentContext()?.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1.0)
            UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
            UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
            UIGraphicsGetCurrentContext()?.strokePath()
            self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
        }
    }
}

extension UINavigationController {    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if let visibleViewController = visibleViewController{
            if visibleViewController.isKind(of: LoginViewController.self) || visibleViewController.isKind(of: HelloViewController.self) || visibleViewController.isKind(of: TransactionStatusViewController.self) || visibleViewController.isKind(of: PrepareViewController.self) || visibleViewController.isKind(of: LensOrderDetailViewController.self) || visibleViewController.isKind(of: ValidateGiftCardViewController.self) || visibleViewController.isKind(of: FidelityCardController.self) || visibleViewController.isKind(of: FirstConfigurationViewController.self){
                return UIInterfaceOrientationMask.portrait
            }
            return visibleViewController.supportedInterfaceOrientations
        }
        if let topViewController = topViewController{
            return topViewController.supportedInterfaceOrientations
        }
        return UIInterfaceOrientationMask.portrait
    }
}
