//
//  GMFaceBottomView.swift
//  Gengmei
//
//  Created by Locus on 2019/9/18.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

import Foundation

import GMBaseSwift

protocol GMFaceBottomViewDelegate: NSObjectProtocol {
    func faceBottomCellDidSelected(_ type: GMScanAnimationType)
}

class GMFaceBottomView: GMView, GMFaceSegmentViewDelegate {
    var selectedType: GMScanAnimationType = .scanFace {
        willSet {
            segmentView.selectedType = newValue
            albumButton.isHidden = newValue == .scanSkin
            courseButton.isHidden = (newValue == .scanFace) || (newValue == .geneMemory)
            shotButton.isEnabled = newValue == .scanFace
        }
    }
    
    weak var delegate: GMFaceBottomViewDelegate?
//    // 底部按钮封装
//    var segmentView: GMFaceShotBottomView!
    // 底部按钮封装
    var segmentView: GMFaceSegmentView!
    // 拍照按钮
    var shotButton: GMButton!
    // 选择相册按钮
    var albumButton: GMFaceButton!
    // 教程按钮
    var courseButton: GMFaceButton!
    // 上次报告
    var lastReportButton: GMFaceButton!
    
    override func setup() {
        let bottomView = GMView()
        bottomView.backgroundColor = .white
        self.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        // 拍照按钮
        shotButton = GMButton(type: .custom)
        shotButton.setImage(UIImage(named: "face_shot_normal"), for: .normal)
        shotButton.setImage(UIImage(named: "face_shot_disabled"), for: .disabled)
        self.addSubview(shotButton)
        shotButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(-62 - UIView.safeAreaInsetsBottom)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 70, height: 70))
        }
        
        segmentView = GMFaceSegmentView()
        segmentView.delegate = self
        self.addSubview(segmentView)
        segmentView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 325, height: 40))
            make.centerX.equalToSuperview()
            make.top.equalTo(shotButton.snp_bottom).offset(10)
        }
        
//        segmentView = GMFaceShotBottomView()
//        segmentView.delegate = self
//        self.addSubview(segmentView)
//        segmentView.snp.makeConstraints { (make) in
//            make.size.equalTo(CGSize(width: 325, height: 40))
//            make.centerX.equalToSuperview()
//            make.top.equalTo(shotButton.snp_bottom).offset(10)
//        }
    
        albumButton = GMFaceButton()
        albumButton.faceLabel.text = "相册"
        albumButton.faceImageView.layer.borderColor = UIColor.separatorLine.cgColor
        albumButton.faceImageView.layer.borderWidth = 1
        albumButton.faceImageView.image = UIImage(named: "NewFacePhotoNone")
        self.addSubview(albumButton)
        albumButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(shotButton)
            make.centerX.equalTo(shotButton).offset(-Constant.screenWidth * 0.25 - 12)
            make.size.equalTo(CGSize(width: 35, height: 52.5))
        }
        
        courseButton = GMFaceButton()
        courseButton.isHidden = true
        courseButton.faceImageView.image = UIImage.init(named: "face_skin_touchbtn")
        courseButton.faceLabel.text = "教程"
        courseButton.addTarget(self, action: #selector(GMFaceBottomView.goTestSkinTeachVC), for: .touchUpInside)
        self.addSubview(courseButton)
        courseButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(shotButton)
            make.centerX.equalTo(shotButton).offset(-Constant.screenWidth * 0.25 - 12)
            make.size.equalTo(CGSize(width: 35, height: 52.5))
        }
        
        lastReportButton = GMFaceButton()
        lastReportButton.faceImageView.image = UIImage.init(named: "face_report_icon")
        lastReportButton.faceLabel.text = "上次报告"
        lastReportButton.addTarget(self, action: #selector(GMFaceBottomView.lastReportButtonBtnClick), for: .touchUpInside)
        self.addSubview(lastReportButton)
        lastReportButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(shotButton)
            make.centerX.equalTo(shotButton).offset(Constant.screenWidth * 0.25 + 12)
            make.size.equalTo(CGSize(width: 44, height: 52.5))
        }
    }
    
    // 上次报告 页面跳转
    @objc func lastReportButtonBtnClick() {
        if selectedType == .scanFace { // 扫脸
            Phobos.track("on_click_button", attributes: ["page_name": "face_scan","button_name": "last_report","tab_name": "AI测脸"]);
            let vc = GMFaceInfoShareViewController()
            AppDelegate.navigation.pushViewController(vc, animated: true)
        } else if selectedType == .scanSkin { // 测肤
            Phobos.track("on_click_button", attributes: ["page_name": "face_scan","button_name": "last_report", "tab_name": "AI测肤"])
            let vc = GMTestSkinAnimationWebView()
            AppDelegate.navigation.pushViewController(vc, animated: true)
        } else if selectedType == .geneMemory { // 面孔起源
            Phobos.track("on_click_button", attributes: ["page_name": "face_scan","button_name": "last_report", "tab_name": "AI面孔"])
            let vc = GMGeneMemoryResultViewController()
            AppDelegate.navigation.pushViewController(vc, animated: true)
        }
    }
    
    // 去测肤页面
    @objc func goTestSkinTeachVC() {
        let testSkinVc = GMTestSkinTeachVC()
        AppDelegate.navigation.pushViewController(testSkinVc, animated: true)
    }
    
    func faceShotBottomCellDidSelected(_ type: GMScanAnimationType) {
        if selectedType != type {
            selectedType = type
            if delegate != nil {
                delegate?.faceBottomCellDidSelected(type)
            }
        }
    }
    
    func segmentViewItemDidSelected(_ type: GMScanAnimationType) {
        if selectedType != type {
            selectedType = type
            if delegate != nil {
                delegate?.faceBottomCellDidSelected(type)
            }
        }
    }
}
