//
//  AlertViewController.swift
//  PaiAi
//
//  Created by FFIB on 2017/11/21.
//  Copyright © 2017年 yb. All rights reserved.
//

import UIKit

protocol AlertAnimatorDelegate: class {
    func getContentView() -> UIView?
}

class AlertViewController: UIViewController, AlertAnimatorDelegate {
    
    enum Style {
        case alert
        case actionSheet
    }
    
    var style: Style {
        return .alert
    }
    
    fileprivate var animator: AlertViewAnimatable = AlertAnimator()
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(red: 52 / 255.0, green: 52 / 255.0, blue: 52 / 255.0, alpha: 0.7)
        configurationGestures()
        
    }
    
    fileprivate func commonInit() {
        modalPresentationStyle = .custom
        transitioningDelegate = self
        
        configurationAnimator()
    }
    
    fileprivate func configurationGestures() {
        configurationTapGesture()
        configurationSwipeGesture()
    }
    
    fileprivate func configurationTapGesture() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(disappear))
        tap.delegate = self
        view.addGestureRecognizer(tap)
    }
    
    fileprivate func configurationSwipeGesture() {
        let swipe = UISwipeGestureRecognizer(target: self, action: #selector(disappear))
        swipe.delegate = self
        swipe.direction = .down
        view.addGestureRecognizer(swipe)
    }
    
    fileprivate func configurationAnimator() {
        switch style {
        case .alert:
            animator = AlertAnimator()
        case .actionSheet:
            animator = ActionSheetAnimator()
        }
    }

    @objc func disappear() {
        dismiss(animated: true, completion: nil)
    }
    
    func getContentView() -> UIView? {
        return nil
    }
}

extension AlertViewController: UIGestureRecognizerDelegate {
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if touch.view == view {
            return true
        }
        return false
    }
}

extension AlertViewController: UIViewControllerTransitioningDelegate {
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let transitioning = DisappearAnimatedTransitioning(animator: animator)
        transitioning.delegate = self
        return transitioning
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let transitioning = AppearAnimatedTransitioning(animator: animator)
        transitioning.delegate = self
        return transitioning
    }
}