//
//  DiscoverHeadPinHeader.swift

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

import JXSegmentedView
class DiscoverHeadPinHeader: UIView {
    let segmentedViewHeight = 48

    var titleLabel: CLLabel!
    var filterButton: EPIconGhostButton!
    lazy var segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: CGFloat(segmentedViewHeight)))
    // lazy var subSegementedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: CGFloat(56)))

    var dataSource = JXSegmentedTitleDataSource()
    // var subDataSouce = JXSegmentedTagStyleDataSource()

    var filterConditionCount : Int = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupData()
        setupEvent()
    }

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

    private func setupViews() {
//        backgroundColor = .random

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

        filterButton = {
            let v = EPIconGhostButton(radius: .none, iconSize: .medium, iconCode: .filterFill)
            v.setBackgroundImage(nil, for: .highlighted)
            addSubview(v)
            v.snp.makeConstraints { make in
                //make.size.equalTo(v.bgImageSize())
                make.trailing.equalToSuperview().offset(-24)
                make.centerY.equalTo(titleLabel)
            }
            return v
        }()

        do {
            addSubview(segmentedView)
            segmentedView.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.top.equalToSuperview().offset(48)
                make.height.equalTo(segmentedViewHeight)
            }
        }

//        do {
//            addSubview(subSegementedView)
//            subSegementedView.snp.makeConstraints { make in
//                make.leading.trailing.equalToSuperview()
//                make.height.equalTo(56)
//                make.top.equalTo(segmentedView.snp.bottom).offset(0)
//            }
//        }

        titleLabel.text = "Classification"
    }

    private func setupData() {
        dataSource.clNormalStyle()
        segmentedView.dataSource = dataSource
        segmentedView.clNormalStyle()

//        subSegementedView.contentEdgeInsetLeft = 24
//        subSegementedView.contentEdgeInsetRight = 24
//        subSegementedView.dataSource = subDataSouce
        
        filterButton.titleLabel?.font = .t.tll
        filterButton.setTitleColor(.text, for: .normal)
        filterButton.touchAreaInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        filterButton.layer.masksToBounds = false
    }

    private func setupEvent() {
        
    }
    
    // MARK: Helper
    
    // MARK: Public
    func setupFilterCount(_ count: Int){
        filterConditionCount = count
        if count > 0{
            filterButton.optionalIconColor = .text
            filterButton.setTitle(" (\(count))", for: .normal)
        }else{
            filterButton.optionalIconColor = .text
            filterButton.setTitle("", for: .normal)
        }
        filterButton.layoutIfNeeded()
    }
}
