//
//  FaceGuideView.swift
//  Pods
//
//  Created by licong on 2017/3/15.
//
//

import UIKit
import GMBaseSwift
import GMKit
import GMCache

protocol ShowGuideProtocol {
    func showShotGuide() -> FaceGuideView
    func showUploadGuide() -> FaceGuideView
    func showAnalyzeResultGuide() -> FaceGuideView
}

extension ShowGuideProtocol where Self: GMBaseController {
    func showShotGuide() -> FaceGuideView {
        let guide = FaceGuideView(frame: view.bounds)
        view.addSubview(guide)
        guide.show(["faceInitGuide2"])
        GMCache.storeObject(atDocumentPathWithkey: FaceGuideView.cacheKey, object: FaceGuideView.cacheKey! as NSCoding)
        self.navigationBar.isHidden = true
        return guide
    }

    func showUploadGuide() -> FaceGuideView {
        let guide = FaceGuideView(frame: view.bounds)
        view.addSubview(guide)
        self.navigationBar.isHidden = true
        guide.show(["faceInitGuide3"])
        GMCache.storeObject(atDocumentPathWithkey: FaceGuideView.cacheKey, object: FaceGuideView.cacheKey! as NSCoding)
        return guide
    }

    func showAnalyzeResultGuide() -> FaceGuideView {
        let guide = FaceGuideView(frame: view.bounds)
        guide.delegate = self as? FaceGuideViewDelegate
        view.addSubview(guide)
        guide.show(["faceInitGuide4"])
        self.navigationBar.isHidden = true
        GMCache.storeObject(atDocumentPathWithkey: FaceGuideView.cacheKey, object: FaceGuideView.cacheKey! as NSCoding)
        return guide
    }
}

@objc protocol FaceGuideViewDelegate: class {
    @objc optional func faceGuideDidDismiss()
}

open class FaceGuideView: GMView {

    weak var delegate: FaceGuideViewDelegate?
    fileprivate static var cacheKey: String!

    //以设备来区分显示到弹不弹出引导,返回为ture表示应该弹guideView
    static func shouldShow(_ deviceKey: String) -> Bool {
        cacheKey = deviceKey
        let obj = GMCache.fetchObject(atDocumentPathWithkey: deviceKey)
        let exsit = (obj != nil)
        return !exsit
    }

    @objc func tapImage() {
        if let view = self.subviews.last {
            view.removeFromSuperview()
            if self.subviews.count == 0 {
                removeFromSuperview()
                #warning("TODO")
//                if let controller = AppDelegate.visibleController as? GMBaseController {
//                    controller.navigationBar.isHidden = false
//                }

                delegate?.faceGuideDidDismiss?()
            }
        }
    }

    func show(_ imageNames: [String]) {

        for name in imageNames.reversed() {
            let image = UIImage(named: name)
            let imageView = UIImageView(image: image)
            imageView.frame = bounds
            addSubview(imageView)
            if #available(iOS 11, *) {
                imageView.contentMode = .scaleAspectFit
            }
            imageView.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(tapImage))
            imageView.addGestureRecognizer(tap)
        }
        if #available(iOS 11, *) {
            self.backgroundColor = UIColor.white
        }
    }
}
