//
//  CountTimerViewController.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/3/28.
//

import UIKit

class MPSideA_CountTimerViewController: UIViewController {
    @IBOutlet weak var timesLabel: UILabel!
    @IBOutlet weak var contentView: UIView!
    //多项选择器
    private lazy var segmentedView:UISegmentedControl = {
        //获取需要展示的内容
        let titles:[String] = timeLevels.map{($0.title)}
        //创建一个SegmentedControl
        let segmentedView:UISegmentedControl = .init(items: titles)
        //设置选中Item颜色块
        if #available(iOS 13.0, *) {
            segmentedView.selectedSegmentTintColor = .init(hex: "#80F988")
        } else {
            segmentedView.tintColor = .init(hex: "#80F988")
        }
        //设置分割线颜色
        segmentedView.setDividerImage(UIImage(), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
        //设置文字颜色
        segmentedView.setTitleTextAttributes([.foregroundColor:UIColor(hex: "#FFFFFF", alpha: 0.8), .font:UIFont.systemFont(ofSize: 14, weight: .medium)], for: .normal)
        segmentedView.setTitleTextAttributes([.foregroundColor:UIColor(hex: "#000000"), .font:UIFont.systemFont(ofSize: 14, weight: .heavy)], for: .selected)
        //添加点击事件
        segmentedView.addTarget(self, action: #selector(switchTimesAction(_ :)), for: .valueChanged)
        return segmentedView
    }()
    //倒计时规模组
    private lazy var timeLevels:[MPSideA_CountTimerLevel] = {
        var array:[MPSideA_CountTimerLevel] = []
        for item in MPSideA_CountTimerLevel.allCases {
            array.append(item)
        }
        return array
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置圆角
        view.layer.masksToBounds = true
        view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        view.layer.cornerRadius = 18*width
        //添加多项选择器
        contentView.addSubview(segmentedView)
        segmentedView.snp.makeConstraints { make in
            make.width.equalTo(330*width)
            make.height.equalTo(38*width)
            make.center.equalToSuperview()
        }
        //根据当前倒计时器设置选中项
        segmentedView.selectedSegmentIndex = MPSideA_MediaCenterManager.shared.getCountTimerLevel().rawValue
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //添加通知
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(timesAction(_ :)), notificationName: .sideA_time_times)
        NotificationCenter.notificationKey.add(observer: self, selector: #selector(completeAction(_ :)), notificationName: .sideA_stop_music)
    }
    deinit {
        //移除所有通知
        NotificationCenter.default.removeObserver(self)
    }
    //切换计时值
    @objc private func switchTimesAction(_ sender: UISegmentedControl) {
        //获取切换后的倒计时规模
        let selectedIndex = sender.selectedSegmentIndex
        let value = timeLevels[selectedIndex]
        //判断该倒计时规模是否成立
        if value != .OFF {
            guard let music = MPSideA_MediaCenterManager.shared.getMusic() else {
                //没有音乐实体，取消倒计时
                print("No Data Music")
                return
            }
            //启动倒计时器
            MPSideA_MediaCenterManager.shared.playerStart(music, actionType: .CountTime, countLevel: value)
        }else {
            //停止倒计时，数值归0
            MPSideA_MediaCenterManager.shared.playerStop()
        }
    }
    
    
    //计时值变化事件
    @objc private func timesAction(_ sender:Notification) {
        DispatchQueue.main.async {
            [weak self] in
            guard let self = self else { return }
            let time = sender.object as! TimeInterval
            timesLabel.text = "Count down "+setTimesToMinSeconds(time)
        }
    }
    //计时结束事件
    @objc private func completeAction(_ sender:Notification) {
        DispatchQueue.main.async {
            [weak self] in
            guard let self = self else { return }
            timesLabel.text = "Timing off"
        }
    }
}
