//
//  KeyboardExchangeCell.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/28.
//

import UIKit
import Kingfisher

class KeyboardExchangeCell: UICollectionViewCell {
    
    static func reuseIdentifier() -> String {
        return "KeyboardExchangeCell"
    }
    
    var keyboardModel: KeyboardModel?
    
    lazy var containerView: UIView = {
       
        let view = UIView()
        view.layer.cornerRadius = 14
        view.backgroundColor = .white
        return view
    }()
    
    lazy var iconImageView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "keyboard_exchange_normal")
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = 8
        return imageView
    }()
    
    lazy var keyboardLabel: UILabel = {
       
        let label = UILabel()
        label.text = "通用键盘"
        label.font = .systemFont(ofSize: 12, weight: .medium)
        label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func config(model: KeyboardModel) {
        
        self.keyboardModel = model
        self.keyboardLabel.text = model.name
        if model.type == "custom" {
            if let url = URL(string: model.imageUrl ?? "") {
                self.iconImageView.kf.setImage(with: url, placeholder: UIImage(named: "keyboard_exchange_normal"))
            }
        } else if model.type == "system" {
            self.iconImageView.image = UIImage(named: "keyboard_exchange_normal")
        }
        
        if model.isChoose == true {
            
            if let image = UIImage.gradient([UIColor.hexStringColor(hexString: "#7D46FC"), UIColor.hexStringColor(hexString: "#BC87FF")], size: CGSize(width: KeyboardExchangeView.UX.cellWidth, height: KeyboardExchangeView.UX.cellHeight), locations: [0, 1], direction: .horizontal) {
                
                containerView.backgroundColor = UIColor(patternImage: image)
            }
            self.keyboardLabel.textColor = .white
        } else {
            
            containerView.backgroundColor = .white
            self.keyboardLabel.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
        }
    }
}

extension KeyboardExchangeCell {
    
    func initUI() {
        
        self.contentView.addSubview(containerView)
        containerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        containerView.addSubview(iconImageView)
        iconImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 36, height: 36))
            make.top.equalTo(10)
            make.centerX.equalToSuperview()
        }
        
        containerView.addSubview(keyboardLabel)
        keyboardLabel.snp.makeConstraints { make in
            make.top.equalTo(iconImageView.snp.bottom).offset(8)
            make.centerX.equalToSuperview()
        }
    }
}
