//
//  JXSegmentedTagStyleCell.swift

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

import JXSegmentedView

class JXSegmentedTagStyleCell: JXSegmentedTitleCell{
    let bgColorView = UIView()
    let bgCornerView = UIView()
    
    open override func commonInit() {
        super.commonInit()

        contentView.insertSubview(bgColorView, at: 0)
        bgColorView.snp.makeConstraints { make in
            //make.edges.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(32)
        }
        
        contentView.insertSubview(bgCornerView, aboveSubview: bgColorView)
        bgCornerView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(32)
        }
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        
//        guard let myItemModel = itemModel as? JXSegmentedTagStyleItemModel else {
//            return
//        }
        
        // Use autolayout
    }
    
    
    open override func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
        super.reloadData(itemModel: itemModel, selectedType: selectedType )
        
        guard let myItemModel = itemModel as? JXSegmentedTagStyleItemModel else {
            return
        }
        
        bgColorView.backgroundColor = itemModel.isSelected ? myItemModel.selectBackgroundColor : myItemModel.normalBackgroundColor
        bgColorView.cornerRadius = myItemModel.backgroundCornerRadius
        
        bgCornerView.cornerRadius = myItemModel.backgroundCornerRadius
        bgCornerView.layer.borderWidth = myItemModel.borderLineWidth
        bgCornerView.layer.borderColor = itemModel.isSelected ? myItemModel.selectBorderColor.cgColor : myItemModel.normalBorderColor.cgColor
        bgCornerView.backgroundColor = .clear
        
        if myItemModel.backgroundHeight != 32{
            bgColorView.snp.updateConstraints { make in
                make.height.equalTo(myItemModel.backgroundHeight)
            }
            bgCornerView.snp.updateConstraints { make in
                make.height.equalTo(myItemModel.backgroundHeight)
            }
        }
        
    }
}
