//
//  MPPositive_TabBarController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/4/19.
//

import UIKit
///b面tabBar控制器
class MPPositive_TabBarController: UITabBarController, UIViewControllerTransitioningDelegate {
    //自定义tabBar
    private lazy var customTabBar:MPPositive_CustomTabBar = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 72*width))
    private lazy var bottomView:MPPositive_BottomShowView = .init(frame: .init(x: 0, y: 0, width: 351, height: 82))
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setValue(customTabBar, forKey: "tabBar")
        //创建标签子控制器
        let homeVC = MPPositive_NavigationController(rootViewController: MPPositive_HomeViewController())
        let searchVC = MPPositive_NavigationController(rootViewController: MPPositive_SearchViewController())
        let libraryVC = MPPositive_NavigationController(rootViewController: MPPositive_LibraryViewController())
        viewControllers = [homeVC,searchVC,libraryVC]
        //禁止系统tabBaritem触发
        tabBar.items?.forEach({ item in
            item.isEnabled = false
        })
        tabBar.barTintColor = .clear
        UITabBar.appearance().backgroundColor = .clear
        tabBar.shadowImage = UIImage()
        tabBar.backgroundImage = UIImage()
        //添加底部播放View
        view.addSubview(bottomView)
        //将展示View设置在主窗口下方之外，避免用户察觉
        bottomView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(82*width)
            make.width.equalTo(351*width)
            make.height.equalTo(82*width)
        }
        bottomView.showListBlock = {
            [weak self] in
            if MP_PlayerManager.shared.loadPlayer != nil {
                MPPositive_ModalType = .PlayerList
                let listVC = MPPositive_PlayerListShowViewController()
                listVC.transitioningDelegate = self
                listVC.modalPresentationStyle = .custom
                self?.present(listVC, animated: true)
            }
        }
        bottomView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(pupPlayerAction)))
        addNotification()
    }
    //监听通知
    private func addNotification() {
        //监听标签切换
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchAction(_:)), notificationName: .switch_tabBarItem)
        //监听控制器弹出
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(pupPlayerAction), notificationName: .pup_player_vc)
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(bottomAnimationAction(_:)), notificationName: .pup_bottom_show)
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(bottomAnimationAction(_:)), notificationName: .player_delete_list)
    }
    deinit {
        //移除所有监听
        NotificationCenter.default.removeObserver(self)
    }
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return MPPositive_PresentationController(presentedViewController: presented, presenting: presenting)
    }
    //弹出音乐播放器
    
}
//MARK: - 通知处理
extension MPPositive_TabBarController {
    //切换事件
    @objc private func switchAction(_ sender:Notification) {
        let tag = sender.object as! Int
        selectedIndex = tag
    }
    //弹出player控制器
    @objc private func pupPlayerAction() {
        DispatchQueue.main.async {
            [weak self] in
            //检索播放器中是否存在load模型
            if MP_PlayerManager.shared.loadPlayer != nil{
                let playerVC = MPPositive_PlayerViewController()
                playerVC.modalPresentationStyle = .fullScreen
                playerVC.recommendBlock = {
                    let recommendVC = MPPositive_RecommendViewController(MP_PlayerManager.shared.loadPlayer.currentVideo.relatedId ?? "")
                    self?.viewControllers![self?.selectedIndex ?? 0].children[0].navigationController?.pushViewController(recommendVC, animated: true)
                }
                self?.present(playerVC, animated: true)
            }
        }
    }
    //切换底部音乐模块状态
    @objc private func bottomAnimationAction(_ sender:Notification) {
        switch_bottomShowAnimation(MP_PlayerManager.shared.loadPlayer != nil)
    }
    //底部BottomView的切换动画
    private func switch_bottomShowAnimation(_ state:Bool) {
        UIView.animate(withDuration: 0.3) {
            [weak self] in
            guard let self = self else { return }
            if state {
                //向上展示
                bottomView.transform = .init(translationX: 0, y: -145*width)
            }else {
                //向下隐藏
                bottomView.transform = .identity
            }
        }
    }
}
