//
//  FilterBarView.swift
//  ColorfulWallpaper
//
//  Created by nkl on 2024/9/10.
//

import Foundation
import UIKit
class FilterBarView: UIView {
    /// default layout items
    lazy var playIcon: UIImageView = UIImageView(image: UIImage(named: "ic_playall"))

    lazy var playButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Play All (0)", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14)
        return btn
    }()

    lazy var mutiButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_mine_ring_muti"), for: .normal)
        return btn
    }()

    lazy var sortButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_mine_ring_sort_down"), for: .normal)
        return btn
    }()

    lazy var selectButton: SpacedButton = {
        let btn = SpacedButton()
        btn.setImage(UIImage(named: "ic_check_n"), for: .normal)
        btn.setImage(UIImage(named: "ic_check_s"), for: .selected)
        btn.setTitle("Select All (0/0)", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14)
        btn.isHidden = true
        btn.spacing = 8
        if FitManager.isAr {
            btn.semanticContentAttribute = .forceLeftToRight
        }
        return btn
    }()

    lazy var searchButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "local_search"), for: .normal)
        return btn
    }()

    lazy var finishMutiButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Done".localized(), for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14)
        btn.setTitleColor(.hexColor("#60FFD2"), for: .normal)
        btn.isHidden = true
        return btn
    }()

    lazy var hStack: UIStackView = .hStack

    var type: MineRingTopType = .default {
        didSet {
            if type == .default {
                defaultlayout()
            } else {
                mutiSelectlayout()
            }
        }
    }

    func defaultlayout() {
        playIcon.isHidden = false
        playButton.isHidden = false
        mutiButton.isHidden = true
        sortButton.isHidden = true
        searchButton.isHidden = true

        selectButton.isHidden = true
        finishMutiButton.isHidden = true
        playButton.isHidden = false
        mutiButton.isHidden = false
        sortButton.isHidden = false
        searchButton.isHidden = true

        selectButton.isHidden = true
        finishMutiButton.isHidden = true
    }

    func mutiSelectlayout() {
        playIcon.isHidden = false
        playButton.isHidden = false
        playButton.isHidden = true
        mutiButton.isHidden = true
        sortButton.isHidden = true
        searchButton.isHidden = true

        selectButton.isHidden = true
        finishMutiButton.isHidden = true
        playIcon.isHidden = true
        playButton.isHidden = true
        mutiButton.isHidden = true
        sortButton.isHidden = true
        searchButton.isHidden = true

        selectButton.isHidden = false
        finishMutiButton.isHidden = false
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addChildren()
        makeConstraint()
    }

    func addChildren() {
        playIcon.isUserInteractionEnabled = true
        addSubview(playIcon)
        addSubview(playButton)
        addSubview(selectButton)

        addSubview(hStack)
        hStack.addArrangedSubview(sortButton)
        hStack.addArrangedSubview(mutiButton)
        hStack.addArrangedSubview(finishMutiButton)
//        hStack.addArrangedSubview(searchButton)
    }

    func makeConstraint() {
        playIcon.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        }

        playButton.snp.makeConstraints { make in
            make.leading.equalTo(playIcon.snp.trailing).offset(8)
            make.centerY.equalTo(playIcon)
            make.height.equalToSuperview()
        }

        hStack.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
        }

        selectButton.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        selectButton.sizeToFit()

        searchButton.snp.makeConstraints { make in
            make.width.height.equalTo(34)
        }

        finishMutiButton.snp.makeConstraints { make in
            make.height.equalTo(34)
        }

        mutiButton.snp.makeConstraints { make in
            make.width.height.equalTo(34)
        }

        sortButton.snp.makeConstraints { make in
            make.width.height.equalTo(34)
        }
    }

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