//
//  MPPositive_PlayListMoreViewController.swift
//  relax.offline.mp3.music
//
//  Created by Mr.Zhou on 2024/6/24.
//

import UIKit

class MPPositive_PlayListMoreViewController: UIViewController, UIViewControllerTransitioningDelegate {
    //小角标图片
    private lazy var indictorImageView:UIImageView = .init(image: .init(named: "Player_Indictor'logo"))
    //标题Label
    private lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 14*width), textColor: .white, textAlignment: .left)
    //EditView
    private lazy var editView:UIView = createNewView("Edit", image: UIImage(named: "Center_Edit'logo")!, tag: 0)
    //DeleteView
    private lazy var deleteView:UIView = createNewView("Delete", image: UIImage(named: "Center_Delete'logo")!, tag: 1)
    //歌单
    private var text:String!
    var editBlock:((String) -> Void)?
    var deleteBlock:(() -> Void)?
    init(_ text:String) {
        super.init(nibName: nil, bundle: nil)
        self.text = text
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .init(hex: "#282A2C")
        view.layer.masksToBounds = true
        view.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        view.layer.cornerRadius = 18*width
        titleLabel.text = text
        configure()
    }
    private func configure() {
        view.addSubview(indictorImageView)
        indictorImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(29*width)
            make.height.equalTo(4*width)
            make.top.equalToSuperview().offset(8*width)
        }
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(40*width)
            make.left.equalToSuperview().offset(18*width)
            make.right.equalToSuperview().offset(-18*width)
        }
        view.addSubview(editView)
        editView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(18*width)
            make.height.equalTo(56*width)
            make.left.right.equalToSuperview()
        }
        view.addSubview(deleteView)
        deleteView.snp.makeConstraints { make in
            make.top.equalTo(editView.snp.bottom)
            make.height.equalTo(56*width)
            make.left.right.equalToSuperview()
        }
    }
    
    //创建一个可点击View
    private func createNewView(_ text:String, image:UIImage, tag:Int) -> UIView {
        let newView:UIView = .init()
        newView.isUserInteractionEnabled = true
        newView.backgroundColor = .clear
        let iconImageView:UIImageView = .init(image: image)
        newView.addSubview(iconImageView)
        iconImageView.snp.makeConstraints { make in
            make.width.height.equalTo(24*width)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(18*width)
        }
        let textLabel = createLabel(text, font: .systemFont(ofSize: 16*width), textColor: .white, textAlignment: .left)
        newView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconImageView.snp.right).offset(18*width)
        }
        newView.tag = tag
        newView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseClick(_ :))))
        return newView
    }
    //点击事件
    @objc private func chooseClick(_ sender:UITapGestureRecognizer) {
        let tag = sender.view?.tag ?? 0
        if tag == 0 {
            //弹出新增自定义框
            MPPositive_ModalType = .NewList
            let listVC = MPSideA_RenameViewController()
            listVC.titleText = "Edit Name"
            listVC.renameBlock = {
                [weak self] text in
                //回调这个text值
                self?.dismiss(animated: true) {
                    if let block = self?.editBlock {
                        block(text)
                    }
                }
            }
            listVC.transitioningDelegate = self
            listVC.modalPresentationStyle = .custom
            present(listVC, animated: true)
        }else {
            dismiss(animated: true) {
                [weak self] in
                if let block = self?.deleteBlock {
                    block()
                }
            }
        }
    }
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return MPPositive_PresentationController(presentedViewController: presented, presenting: presenting)
    }
}
