//
//  PQSelectedOprationView.swift
//  PQSpeed
//
//  Created by SanW on 2020/12/11.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit
import BFCommonKit

// MARK: - 选择操作view

/// 选择操作view
public class PQSelectedOprationView: UIView {
    public let itemH: CGFloat = cDefaultMargin * 6
    public  let lineH: CGFloat = 1
    public  let margin: CGFloat = 8
    public  var contentH: CGFloat = 0
    public  var itemList: [String]? {
        didSet {
            if (itemList?.count ?? 0) > 0 {
                contentH = itemH + margin + cSafeAreaHeight + CGFloat(itemList?.count ?? 0) * (itemH + 1)
                contentView.frame = CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: contentH)
                contentView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 11)
                cancelBtn.frame = CGRect(x: 0, y: contentH - itemH - cSafeAreaHeight, width: cScreenWidth, height: itemH + cSafeAreaHeight)
                addSubViews()
            }
        }
    }

    public  var completeHander: ((_ sender: UIButton) -> Void)?
    lazy public var contentView: UIView = {
        let contentView = UIView(frame: CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: itemH + margin))
        contentView.backgroundColor = UIColor.black
        contentView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 11)
        return contentView
    }()

    lazy public var cancelBtn: UIButton = {
        let cancelBtn = UIButton(type: .custom)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#BDBDBD"), for: .normal)
        cancelBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#2C2C2C")
        cancelBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        return cancelBtn
    }()

    override public init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = cShadowColor
        addSubview(contentView)
        contentView.addSubview(cancelBtn)
        let ges = UITapGestureRecognizer(target: self, action: #selector(removeView))
        addGestureRecognizer(ges)
    }

    required public init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func layoutSubviews() {
        super.layoutSubviews()
//        addSubViews()
    }

    public func addSubViews() {
        if (itemList?.count ?? 0) > 0 {
            for (index, item) in itemList!.enumerated() {
                let normalBtn = UIButton(type: .custom)
                normalBtn.frame = CGRect(x: 0, y: (itemH + 1) * CGFloat(index), width: frame.width, height: itemH)
                normalBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#2C2C2C")
                normalBtn.setTitle(item, for: .normal)
                normalBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#BDBDBD"), for: .normal)
                normalBtn.tag = index + 1
                normalBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
                normalBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
                contentView.addSubview(normalBtn)

                let lineView = UIView(frame: CGRect(x: 0, y: normalBtn.frame.maxY, width: frame.width, height: lineH))
                lineView.backgroundColor = UIColor.hexColor(hexadecimal: "#212223")
                contentView.addSubview(lineView)
                contentH = contentH + (itemH + 1)
            }
        }
    }

    @objc public func removeView() {
        UIView.animate(withDuration: 0.3, animations: { [weak self] in
            self?.contentView.frame = CGRect(x: 0, y: cScreenHeigth, width: cScreenWidth, height: self!.contentH)
        }) { [weak self] _ in
            self?.removeFromSuperview()
        }
    }

    class public func showSelectedOprationView(itemList: [String], completeHander: @escaping ((_ sender: UIButton) -> Void)) -> PQSelectedOprationView  {
        let selectedOprationView: PQSelectedOprationView = PQSelectedOprationView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
        selectedOprationView.completeHander = completeHander
        selectedOprationView.itemList = itemList
        UIApplication.shared.keyWindow?.addSubview(selectedOprationView)
        let contentH: CGFloat = 68.0 + cSafeAreaHeight + CGFloat(itemList.count) * 61.0
        UIView.animate(withDuration: 0.3, animations: {
            selectedOprationView.contentView.frame = CGRect(x: 0, y: cScreenHeigth - contentH, width: cScreenWidth, height: contentH)
        }) { _ in
        }
        return selectedOprationView
    }

    @objc public func btnClick(sender: UIButton) {
        if completeHander != nil {
            completeHander!(sender)
        }
        removeView()
    }
}
