//
//  LNHomeGameTabItemView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/14.
//

import Foundation
import UIKit
import SnapKit


class LNHomeGameTabItemView: UIView {
    private let cover = UIImageView()
    private let selectedBorder = UIImageView()
    private let nameLabel = UILabel()
    
    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) {
        selectedBorder.image = selected ? .primary_7 : nil
        nameLabel.font = selected ? .heading_h5 : .body_s
    }
    
    func toBeMoreTab() {
        selectedBorder.isHidden = true
        
        cover.layer.borderColor = UIColor.clear.cgColor
        cover.image = .icHomeGameTabMore
        cover.clipsToBounds = false
        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 LNHomeGameTabItemView {
    private func setupViews() {
        selectedBorder.backgroundColor = .fill
        selectedBorder.layer.cornerRadius = 26
        selectedBorder.clipsToBounds = true
        insertSubview(selectedBorder, belowSubview: cover)
        selectedBorder.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.height.equalTo(50)
            make.leading.greaterThanOrEqualToSuperview()
        }
        
        cover.backgroundColor = .fill
        cover.layer.cornerRadius = 24
        cover.clipsToBounds = true
        addSubview(cover)
        cover.snp.makeConstraints { make in
            make.edges.equalTo(selectedBorder).inset(1)
        }
        
        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.centerX.equalToSuperview()
            make.top.equalTo(selectedBorder.snp.bottom).offset(3)
            make.bottom.equalToSuperview()
            make.leading.equalToSuperview().offset(6)
        }
    }
}
