//
//  INIntrocudeController.swift
//  Introduce
//
//  Created by 胡志强 on 2021/11/29.
//

import BFCommonKit
import BFRecordScreenKit
import BFUIKit
import Foundation
import Photos
import UIKit

class INIntroduceController: BFBaseViewController {
    var stripSwithView: BFStripSwithView?
    let exportBtn = UIButton()
    var assets: [PHAsset]? {
        didSet {
            if let ass = assets {
                recordScreenVC.assets = ass
            }
        }
    }

    let recordScreenVC = BFRecordScreenController()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        showNavigation()

        PQNotification.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        PQNotification.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        PQNotification.removeObserver(self)
    }

    @objc internal func keyboardWillHide(_: Notification?) {
        navHeadImageView?.isHidden = false
    }

    @objc internal func keyboardWillShow(_: Notification?) {
        navHeadImageView?.isHidden = true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        navHeadImageView?.backgroundColor = .clear
        leftButton(image: nil, imageName: nil, tintColor: .white)

        exportBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#28BE67")
        exportBtn.setTitle("导出", for: .normal)
        exportBtn.addCorner(corner: 4)
        exportBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        exportBtn.addTarget(self, action: #selector(exportAction), for: .touchUpInside)
        navHeadImageView?.addSubview(exportBtn)
        exportBtn.frame = CGRect(x: (navHeadImageView?.frame.width ?? 0) - 12 - 60, y: 0, width: 60, height: 36)
        exportBtn.center.y = backButton?.center.y ?? 0

        addChild(recordScreenVC)
        recordScreenVC.view.frame = view.frame
        view.addSubview(recordScreenVC.view)
        recordScreenVC.changeItemHandle = { [weak self] index in
            self?.stripSwithView?.changeSwitchStatus(index: index)
        }
        if assets != nil, (assets?.count ?? 0) > 1 {
            stripSwithView = BFStripSwithView(frame: CGRect(x: (backButton?.frame.maxX ?? 0), y: 0, width: exportBtn.frame.minX - (backButton?.frame.maxX ?? 0) - 10, height: cDevice_iPhoneNavBarHei), items: assets?.count ?? 1)
            stripSwithView?.center.y = backButton?.center.y ?? 0
            stripSwithView?.itemClickHandle = { [weak self] _, index in
                self?.recordScreenVC.updateContentOffset(index: index)
            }
            navHeadImageView?.addSubview(stripSwithView!)
        }
    }

    override func backBtnClick() {
        let remindData = BFBaseModel()
        remindData.summary = "退出将不会保存当前操作"
        let alertV = BFRemindView(frame: view.bounds)
        alertV.isBanned = true
        alertV.confirmBtn.setTitle("不退出", for: .normal)
        alertV.confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        alertV.cancelBtn.setTitle("确认退出", for: .normal)
        alertV.cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        alertV.contentLab.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        alertV.remindData = remindData
        alertV.confirmBtn.setTitleColor(UIColor.black, for: .normal)
        alertV.cancelBtn.setTitleColor(UIColor.red, for: .normal)
        alertV.contentLab.textColor = .black
        alertV.remindBlock = { [weak self] item, _ in
            if item.tag == 1 { // 确定返回到上一层
                self?.recordScreenVC.backBtnClick()
                self?.super_back()
            }
        }
        UIApplication.shared.keyWindow?.addSubview(alertV)
    }

    private func super_back() {
        super.backBtnClick()
    }

    @objc func exportAction() {
        recordScreenVC.backBtnClick()

        let controller = INVideoExportController()
        controller.export.data = recordScreenVC.itemModels
        navigationController?.pushViewController(controller, animated: true)
    }
}
