//
//  MPPositive_PlayerCoverView.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/8.
//

import UIKit
//B面播放器封面View(封面，标题，副标题，收藏，下载，进度条View)
class MPPositive_PlayerCoverView: UIView {
    ///封面
    lazy var coverImageView:UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 16*width
        return imageView
    }()
    ///标题
    lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 22*width, weight: .regular), textColor: .init(hex: "#FFFFFF", alpha: 0.85), textAlignment: .left)
    ///副标题
    lazy var subtitleLabel:UILabel = createLabel(font: .systemFont(ofSize: 12*width, weight: .regular), textColor: .init(hex: "#EEEEEE", alpha: 0.6), textAlignment: .left)
    ///收藏按钮
    lazy var collectionSongBtn:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "List_UnCollection'logo"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "List_Collectioned'logo"), for: .selected)
        btn.addTarget(self, action: #selector(collectionSwitchClick(_ :)), for: .touchUpInside)
        return btn
    }()
    ///下载按钮
    lazy var loadBtn:UIButton = {
        let btn:UIButton = .init()
        btn.setBackgroundImage(UIImage(named: "Song_Unload'logo"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .selected)
        btn.addTarget(self, action: #selector(loadActionClick(_ :)), for: .touchUpInside)
        return btn
    }()
    ///进度条View
    lazy var sliderView:MPPositive_PlayerSilder = {
        let sliderView:MPPositive_PlayerSilder = .init(frame: .init(x: 0, y: 0, width: 335*width, height: 6*width))
        sliderView.addTarget(self, action: #selector(seekProgressClick(_:forEvent:)), for: .valueChanged)
        sliderView.addTarget(self, action: #selector(seekProgressClick(_:forEvent:)), for: .touchDown)
        sliderView.addTarget(self, action: #selector(seekProgressClick(_:forEvent:)), for: .touchUpInside)
        return sliderView
    }()
    ///当前播放时间值Label
    lazy var durationLabel:UILabel = createLabel("00:00" ,font: .systemFont(ofSize: 12*width, weight: .medium), textColor: .init(hex: "#FFFFFF", alpha: 0.85), textAlignment: .left)
    ///最大播放时间值Label
    lazy var maxTimesLabel:UILabel = createLabel("00:00" ,font: .systemFont(ofSize: 12*width, weight: .medium), textColor: .init(hex: "#FFFFFF", alpha: 0.6), textAlignment: .right)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        configure()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    //视图配置
    private func configure() {
        //配置封面图
        addSubview(coverImageView)
        coverImageView.snp.makeConstraints { make in
            make.width.equalTo(335*width)
            make.height.equalTo(330*width)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(12*width)
        }
        //添加标题
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(coverImageView.snp.left)
            make.top.equalTo(coverImageView.snp.bottom).offset(36*width)
            make.right.equalTo(coverImageView.snp.right).offset(-100*width)
        }
        addSubview(subtitleLabel)
        subtitleLabel.snp.makeConstraints { make in
            make.left.right.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(6*width)
        }
        //配置下载和收藏按钮
        addSubview(loadBtn)
        loadBtn.snp.makeConstraints { make in
            make.right.equalTo(coverImageView.snp.right).offset(-12*width)
            make.top.equalTo(coverImageView.snp.bottom).offset(47*width)
            make.width.height.equalTo(24*width)
        }
        addSubview(collectionSongBtn)
        collectionSongBtn.snp.makeConstraints { make in
            make.right.equalTo(loadBtn.snp.left).offset(-20*width)
            make.centerY.equalTo(loadBtn.snp.centerY)
            make.width.height.equalTo(24*width)
        }
        //配置进度条和时间label
        addSubview(sliderView)
        sliderView.snp.makeConstraints { make in
            make.top.equalTo(subtitleLabel.snp.bottom).offset(25*width)
            make.centerX.equalToSuperview()
            make.width.equalTo(335*width)
            make.height.equalTo(6*width)
        }
        addSubview(durationLabel)
        durationLabel.snp.makeConstraints { make in
            make.left.equalTo(sliderView.snp.left)
            make.top.equalTo(sliderView.snp.bottom).offset(5*width)
        }
        addSubview(maxTimesLabel)
        maxTimesLabel.snp.makeConstraints { make in
            make.right.equalTo(sliderView.snp.right)
            make.top.equalTo(sliderView.snp.bottom).offset(5*width)
        }
    }
    //调整音乐进度
    @objc private func seekProgressClick(_ sender: UISlider, forEvent event: UIEvent) {
        //获取touchEvent
        let touchEvent = event.allTouches?.first
        //判断点击事件状态
        switch touchEvent?.phase {
        case .began://开始拖动
            //让播放器进入调整状态
            MP_PlayerManager.shared.setEditPorgressStatu()
        case .moved://移动中
            break
        case .ended://结束
            let value = sender.value
            //让播放器恢复状态
            MP_PlayerManager.shared.setEditProgressEnd(value)
        default:
            break
        }
    }
    //切换当前列表收藏状态
    @objc private func collectionSwitchClick(_ sender:UIButton) {
        
    }
    //点击下载
    @objc private func loadActionClick(_ sender:UIButton) {
        //下载，检索当前播放音乐是否存在
        if MP_PlayerManager.shared.loadPlayer.currentVideo != nil {
            //当前音乐存在，执行音乐下载
            //处理完成
            let item = MPPositive_DownloadItemModel.create()
            
            MPPositive_DownloadItemModel.save()
        }
    }
}
