//
//  MPPresentationController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/3/27.
//

import UIKit
///自定义模态弹出类型
enum MPSideA_PresentModal{
    ///倒计时选择框
    case Timer
    ///新增选项框
    case Choice
    ///重命名
    case Rename
    ///删除
    case Delete
}

///自定义模态弹出控制器
class MPSideA_PresentationController: UIPresentationController {
    //蒙板
    fileprivate lazy var maskView: UIView = {
        let corverView = UIView()
        return corverView
    }()
    // MARK: - 系统回调(重载)
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        //根据模态状态确定弹出控制器的布局
        switch MPSideA_ModalType {
        case .Timer:
            presentedView?.snp.makeConstraints({ (make) in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(180*width)
            })
        case .Choice:
            presentedView?.snp.makeConstraints({ (make) in
                make.bottom.left.right.equalToSuperview()
                make.height.equalTo(165*width)
            })
        case .Rename:
            presentedView?.snp.makeConstraints({ (make) in
                make.center.equalToSuperview()
                make.width.equalTo(270*width)
                make.height.equalTo(178*width)
            })
        case .Delete:
            presentedView?.snp.makeConstraints({ (make) in
                make.center.equalToSuperview()
                make.width.equalTo(270*width)
                make.height.equalTo(122*width)
            })
        }
        //添加蒙版
        setMask()
    }
    // 添加蒙版
    fileprivate func setMask() -> Void {
        // 添加蒙版
        containerView?.insertSubview(maskView, at: 0)
        //设置蒙版的属性
        maskView.backgroundColor = UIColor(white: 0.0, alpha: 0.8)
        maskView.frame = containerView!.bounds
        let tap = UITapGestureRecognizer(target: self, action: #selector(maskClick(_:)))
        maskView.addGestureRecognizer(tap)
    }
    //蒙板点击事件
    @objc fileprivate func maskClick(_ sender:UITapGestureRecognizer){
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}
