//
//  LNHomeActivityTabItemView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/21.
//

import Foundation
import UIKit
import SnapKit


class LNHomeActivityTabItemView: UIView {
    private let cover = UIImageView()
    private let nameLabel = UILabel()
    private let selectedIc = UIImageView()
    
    private(set) var item: LNGameCategoryItemVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ item: LNGameCategoryItemVO) {
        cover.sd_setImage(with: URL(string: item.icon))
        nameLabel.text = item.name
        self.item = item
    }
    
    func setSelected(_ selected: Bool) {
        let image: UIImage = selected ? .icHomeActivityTabSelected : .icHomeActivityTab
        selectedIc.image = image.resizableImage(
            withCapInsets: .init(top: 50, left: 50, bottom: 40, right: 50), resizingMode: .stretch
        )
        nameLabel.font = selected ? .heading_h5 : .body_s
    }
    
    func toBeMoreTab() {
        selectedIc.snp.remakeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview().inset(4)
            make.trailing.equalToSuperview().offset(-3)
        }
        cover.image = .icHomeGameTabMore
        cover.contentMode = .center
        cover.snp.remakeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(selectedIc)
        }
        
        nameLabel.text = .init(key: "A00048")
        nameLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        nameLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNHomeActivityTabItemView {
    private func setupViews() {
        addSubview(selectedIc)
        selectedIc.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(0).priority(.low)
        }
        
        addSubview(cover)
        cover.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.width.height.equalTo(58)
            make.trailing.equalTo(selectedIc.snp.trailing).offset(17)
        }
        
        nameLabel.textAlignment = .center
        nameLabel.font = .body_s
        nameLabel.textColor = .text_5
        nameLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
        nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(9)
            make.trailing.lessThanOrEqualTo(cover.snp.leading).offset(2)
        }
    }
}
