//
//  MPPositive_ArtistShowHeaderView.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/15.
//

import UIKit
import Kingfisher
class MPPositive_ArtistShowHeaderView: UIView {
    //艺术家图片
    private lazy var reviewImageView:UIImageView = {
        let imageView:UIImageView = .init()
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()
    var  artistid:String = ""
    var iscollection:Bool = false
    //艺术家名字
    private lazy var nameLabel:UILabel = createLabel(font: .systemFont(ofSize: 32*width, weight: .semibold), textColor: .white, textAlignment: .left)
    //艺术家当前订阅量label
    private lazy var followersLabel:UILabel = createLabel(font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .white, textAlignment: .left)
    //是否收藏艺术家按钮
    private lazy var collectionBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Artist_Collection'logo"), for: .normal)
        btn.setImage(UIImage(named: "Artist_Collectioned'logo"), for: .selected)
        btn.setBackgroundImage(UIImage(named: "Artist_Collection'bg"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "Artist_Collectioned'bg"), for: .selected)
        btn.addTarget(self, action: #selector(collectionClick(_ :)), for: .touchUpInside)
        return btn
    }()
    //阴影
    private lazy var maskImageView:UIImageView = {
        let imageView:UIImageView = .init(image: .init(named: "List_Cover'mask"))
        imageView.contentMode = .scaleAspectFill
        imageView.isUserInteractionEnabled = false
        return imageView
    }()
    var artist:MPPositive_ArtistViewModel!{
        didSet{
            reviewImageView.kf.setImage(with: URL(string: artist.header?.thumbnails?.last ?? ""), placeholder: placeholderImage)
            nameLabel.text = artist.header?.title
            followersLabel.text = (artist.header?.subscriptions ?? "")+" "+(artist.header?.subscriptionedText ?? "")
            let items = MPPositive_CollectionArtistModel.fetch(.init(format: "artistId == %@", self.artistid))
            for item in items {
                if item.artistId == self.artistid {
                 
                        self.collectionBtn.isSelected = true
                    
                }
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        configure()

    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
    }
    private func configure() {
        addSubview(reviewImageView)
        reviewImageView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
        addSubview(maskImageView)
        maskImageView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(225*width)
        }
        addSubview(collectionBtn)
        collectionBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-18*width).priority(999)
            make.bottom.equalToSuperview().offset(-50*width).priority(999)
            make.width.height.equalTo(72*width)
        }
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18*width).priority(999)
            make.bottom.equalToSuperview().offset(-80*width).priority(999)
            make.right.equalTo(collectionBtn.snp.left).offset(-10*width).priority(999)
        }
        addSubview(followersLabel)
        followersLabel.snp.makeConstraints { make in
            make.left.right.equalTo(nameLabel)
            make.bottom.equalTo(collectionBtn)
        }

    }
    //收藏这位艺术家
    @objc private func collectionClick(_ sender:UIButton) {
        if self.collectionBtn.isSelected == true{
            self.collectionBtn.isSelected = false
            
//            MPPositive_CollectionArtistModel.fetch(.init(format: "artistId == %@", self.browseid)).forEach { i in
//                if i.artistId == self.browseid{
//                        MPPositive_CollectionArtistModel.delete(i)
//                    }
//                }
          let  items =  MPPositive_CollectionArtistModel.fetch(.init(format: "artistId == %@", self.artistid))
            for item in items {
                if item.artistId == self.artistid {
                    MPPositive_CollectionArtistModel.delete(item)
                }
            }

            
        }else{
            self.collectionBtn.isSelected = true
            let item = MPPositive_CollectionArtistModel.create()
            item.title = artist.header.title
            item.coverImage = URL(string: artist.header.thumbnails?.last ?? "")
            item.subtitle = artist.header.subscriptionedText
            item.artistId = self.artistid
            MPPositive_CollectionArtistModel.save()
            
        }
        
    }
}
