//
//  RoleFilterPieces.swift

//
//  Created by Leon on 2025/9/9.
//

enum RoleFilterTypeItemType:String {
    case original = "R00002"
    case anime = "R00004"
    case game = "R00005"
    case filmAndTv = "R00006"
}

class RoleFilterTypeItem: UIView {
    var gradientBg: GradientView!
    var titleLabel: CLLabel!

    var figureImageView: CLImageView!
    
    var topButton: UIButton!

    var type: RoleFilterTypeItemType! = .original
    var selected : Bool = false {
        didSet{
            setupSelected(selected)
        }
    }

    override private init(frame: CGRect) {
        super.init(frame: frame)
    }

    convenience init(type: RoleFilterTypeItemType) {
        self.init(frame: .zero)
        self.type = type
        setupViews()
    }

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

    private func setupViews() {
        backgroundColor = .c.csen
        cornerRadius = 16
        layer.borderWidth = 1
        layer.borderColor = UIColor.c.con.cgColor

        var colors = [UIColor]()
        switch type {
        case .original:
            colors = [UIColor(hex: "#62E1F2"), UIColor(hex: "#6296F2")]
        case .anime:
            colors = [UIColor(hex: "#E03CE6").withAlphaComponent(0.5), UIColor(hex: "#E63C8B").withAlphaComponent(0.5)]
        case .game:
            colors = [UIColor(hex: "#475CFF").withAlphaComponent(0.5), UIColor(hex: "#7B47FF").withAlphaComponent(0.5)]
        case .filmAndTv:
            colors = [UIColor(hex: "#FF6464").withAlphaComponent(0.5), UIColor(hex: "#FFA264").withAlphaComponent(0.5)]
        default:
            break
        }

        gradientBg = {
            let v = GradientView(colors: colors, gradientType: .topToBottom)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            v.isHidden = true
            return v
        }()

        titleLabel = {
            let v = CLLabel()
            v.font = .t.tlm
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(8)
                make.leading.equalToSuperview().offset(12)
                make.trailing.equalToSuperview().offset(-8)
            }
            return v
        }()

        figureImageView = {
            let v = CLImageView()
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(0) // 28
                make.top.equalToSuperview().offset(36)
                make.bottom.trailing.equalToSuperview()
            }
            return v
        }()
        
        topButton = {
            let v = UIButton()
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()

        switch type {
        case .original:
            titleLabel.text = "Original"
            figureImageView.image = UIImage(named: "role_filter_img_oc")
        case .anime:
            titleLabel.text = "Anime"
            figureImageView.image = UIImage(named: "role_filter_img_anime")
        case .game:
            titleLabel.text = "Game"
            figureImageView.image = UIImage(named: "role_filter_img_game")
        case .filmAndTv:
            titleLabel.text = "Film&TV"
            figureImageView.image = UIImage(named: "role_filter_img_filmTV")
        default:
            break
        }
    }

    private func setupSelected(_ selected: Bool) {
        gradientBg.isHidden = !selected
    }
}

class RoleFilterTypeView: UIView {
    var bigTitle: CLLabel!

    var subTitleLabel1: CLLabel!
    var stackH1: UIStackView!

    var subTitleLabel2: CLLabel!
    var stackH2: UIStackView!

    var item1: RoleFilterTypeItem!
    var item2: RoleFilterTypeItem!
    var item3: RoleFilterTypeItem!
    var item4: RoleFilterTypeItem!
    
    var items = [RoleFilterTypeItem]()
    
    var selectType:RoleFilterTypeItemType?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

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

    private func setupViews() {
        let itemWidth = floor((UIScreen.width - 24 * 2 - 12 * 2) / 3.0)
        let itemHeight = itemWidth

        bigTitle = {
            let v = CLLabel()
            v.font = .t.ttm
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.leading.equalToSuperview()
            }
            return v
        }()

        subTitleLabel1 = {
            let v = CLLabel()
            v.font = .t.tls
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview()
                make.top.equalTo(bigTitle.snp.bottom).offset(20)
            }
            return v
        }()

        stackH1 = {
            let v = UIStackView()
            v.distribution = .fillEqually
            v.spacing = 12
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.top.equalTo(subTitleLabel1.snp.bottom).offset(12)
                make.height.equalTo(itemHeight)
            }
            return v
        }()

        item1 = {
            let v = RoleFilterTypeItem(type: .original)
            stackH1.addArrangedSubview(v)
            return v
        }()

        stackH1.addArrangedSubview(UIView())
        stackH1.addArrangedSubview(UIView())

        subTitleLabel2 = {
            let v = CLLabel()
            v.font = .t.tls
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview()
                make.top.equalTo(stackH1.snp.bottom).offset(20)
            }
            return v
        }()

        stackH2 = {
            let v = UIStackView()
            v.distribution = .fillEqually
            v.spacing = 12
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.top.equalTo(subTitleLabel2.snp.bottom).offset(12)
                make.height.equalTo(itemHeight)
                make.bottom.equalToSuperview().offset(-16)
            }
            return v
        }()

        item2 = {
            let v = RoleFilterTypeItem(type: .anime)
            stackH2.addArrangedSubview(v)
            return v
        }()

        item3 = {
            let v = RoleFilterTypeItem(type: .game)
            stackH2.addArrangedSubview(v)
            return v
        }()

        item4 = {
            let v = RoleFilterTypeItem(type: .filmAndTv)
            stackH2.addArrangedSubview(v)
            return v
        }()

        bigTitle.text = "Type"
        #warning("to do, 这里的文案最好用字典中的！")
        subTitleLabel1.text = "Original"
        subTitleLabel2.text = "Fanfiction"
        
        items.append(item1)
        items.append(item2)
        items.append(item3)
        items.append(item4)
        
        item1.topButton.addTarget(self, action: #selector(tapTypeButton(button:)), for: .touchUpInside)
        item2.topButton.addTarget(self, action: #selector(tapTypeButton(button:)), for: .touchUpInside)
        item3.topButton.addTarget(self, action: #selector(tapTypeButton(button:)), for: .touchUpInside)
        item4.topButton.addTarget(self, action: #selector(tapTypeButton(button:)), for: .touchUpInside)
    }
    
    // MARK: - Public
    
    func selectTypes(_ types: [String]?){
        guard let roleCodes = types else{return}
        for per in roleCodes {
            if per == RoleFilterTypeItemType.original.rawValue{
                item1.selected = true
            }else if per == RoleFilterTypeItemType.anime.rawValue{
                item2.selected = true
            }else if per == RoleFilterTypeItemType.game.rawValue{
                item3.selected = true
            }else if per == RoleFilterTypeItemType.filmAndTv.rawValue{
                item4.selected = true
            }
        }
        
    }
    
    func getSelectedTypes() -> [String] {
        var selectedTypes: [String] = []
        for item in items {
            if item.selected {
                selectedTypes.append(item.type.rawValue)
            }
        }
        return selectedTypes
    }
    
    // MARK: - Action
    
    @objc private func tapTypeButton(button:UIButton){
        guard let superView = button.superview else { return }
        
        let index = items.firstIndex(of: superView as! RoleFilterTypeItem)
        if let index = index {
            let item  = items[index]
            item.selected = !item.selected
        }
    }
}
