//
//  CWOperateViewController.swift
//  ColorfulWallpaper
//
//  Created by ni on 2024/9/19.
//

import Foundation
import TSVideoKit

enum CWOperateType {
    case like
    case addPlaylist
    case share
    case deleteVideo
    case rename
    case deletePlaylist
    case removeFromPlaylist
}

class CWOperateViewController: LWBaseViewController {
    var operteItem: TSVideo?
    var playlist: TSPlayList?

    var disAppearArea: UIControl = .init()
    lazy var songlistCell: OperateTopView = OperateTopView()
    lazy var bgView: UIView = .simpleView(color: .hexColor("#222222"))
    lazy var vStack: UIStackView = .vStack
    lazy var likeItem: CWOperateItemView = .init(imgName: "ic_like_n", title: "Add to like".localized(), type: .like)
    lazy var addPlaylistItem: CWOperateItemView = .init(imgName: "ic_add_playlist", title: "Add to Playlist".localized(), type: .addPlaylist)
    lazy var removeFromPlaylist: CWOperateItemView = .init(imgName: "ic_remove_playlist", title: "Remove from playlist".localized(), type: .removeFromPlaylist)
    lazy var shareItem: CWOperateItemView = .init(imgName: "ic_share", title: "Share".localized(), type: .share)
    lazy var deleteItem: CWOperateItemView = .init(imgName: "ic_delete", title: "Delete".localized(), type: .deleteVideo)

    var types: [CWOperateType] = [.like, .addPlaylist, .share, .deleteVideo]

    lazy var cancelButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Cancel".localized(), for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont14
        btn.addTarget(self, action: #selector(dismissCurrentVc), for: .touchUpInside)
        return btn
    }()

    @objc func dismissCurrentVc() {
        dismiss(animated: true)
    }

    init(types: [CWOperateType] = [.like, .addPlaylist, .share, .deleteVideo]) {
        super.init(nibName: nil, bundle: nil)
        self.types = types
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        if let model = operteItem {
            songlistCell.bindData(video: model)
        }
        super.viewDidLoad()
        addTargets()
        updateLikeInfo(item: likeItem)
    }

    override func addChildren() {
        super.addChildren()
        view.backgroundColor = .black.withAlphaComponent(0.4)
        view.addSubview(disAppearArea)
        bgView.layer.cornerRadius = 16
        bgView.layer.masksToBounds = true
        view.addSubview(bgView)
        bgView.addSubview(songlistCell)
        bgView.addSubview(vStack)
        vStack.addArrangedSubview(likeItem)
        vStack.addArrangedSubview(addPlaylistItem)
        vStack.addArrangedSubview(removeFromPlaylist)
        vStack.addArrangedSubview(shareItem)
        vStack.addArrangedSubview(deleteItem)
        vStack.addArrangedSubview(cancelButton)
        vStack.arrangedSubviews.forEach {
            if let item = $0 as? CWOperateItemView, types.contains(item.type) {
                $0.isHidden = false
            } else {
                $0.isHidden = true
            }
        }
    }

    override func makeConstarints() {
        super.makeConstarints()
        disAppearArea.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.bottom.equalTo(bgView.snp.top)
        }
        bgView.snp.makeConstraints { make in
            make.bottom.leading.trailing.equalToSuperview()
        }

        songlistCell.snp.makeConstraints { make in
            make.height.equalTo(64)
            make.top.leading.trailing.equalToSuperview()
        }

        vStack.snp.makeConstraints { make in
            make.top.equalTo(songlistCell.snp.bottom)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
        cancelButton.snp.makeConstraints { make in
            make.height.equalTo(44)
        }
    }

    func updateLikeInfo(item: CWOperateItemView) {
        let favIcon = operteItem?.isFavorite == true ? UIImage(
            named: "ic_like_s") : UIImage(named: "ic_like")
        item.iconView.image = favIcon
        item.titleLabel.text = operteItem?.isFavorite == true ? "Remove from like".localized() : "Add to like".localized()
    }
}
