//
//  LNIMChatGameMateSkillCell.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/11.
//

import Foundation
import UIKit
import SnapKit


protocol LNIMChatGameMateSkillCellDelegate: AnyObject {
    func onIMChatGameMateSkillCell(cell: LNIMChatGameMateSkillCell, didClickOrder skill: LNGameMateSkillVO)
}


class LNIMChatGameMateSkillCell: UIView {
    private let background = UIImageView()
    private let gameIc = UIImageView()
    
    private let gameView = UIView()
    private let discountView = LNNewbieDiscountView()
    private let gameNameLabel = UILabel()
    private let gamePriceLabel = UILabel()
    
    private let toVoiceButton = UIButton()
    
    private var curItem: LNGameMateSkillVO?
    
    weak var delegate: LNIMChatGameMateSkillCellDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ skill: LNGameMateSkillVO) {
        gameView.isHidden = false
        
        gameIc.sd_setImage(with: URL(string: skill.icon))
        gameNameLabel.text = skill.name
        gamePriceLabel.text = "\(skill.price.toDisplay) / \(skill.unit)"
        curItem = skill
        
        updateDiscount()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNIMChatGameMateSkillCell: LNOrderManagerNotify {
    func onMyDiscountInfoChanged(info: LNOrderDiscountVO?) {
        updateDiscount()
    }
    
    private func updateDiscount() {
//        if let discount = LNOrderManager.shared.discountFor(curItem?.price ?? 0) {
//            discountView.isHidden = false
//            discountView.update(1 - discount)
//        } else {
            discountView.isHidden = true
//        }
    }
}

extension LNIMChatGameMateSkillCell {
    private func setupViews() {
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 12
        container.layer.borderWidth = 1
        container.layer.borderColor = UIColor.fill.cgColor
        addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(4)
            make.verticalEdges.equalToSuperview()
        }
        
        background.layer.cornerRadius = 11
        background.clipsToBounds = true
        background.image = .primary_6
        background.alpha = 0.8
        container.addSubview(background)
        background.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(1).priority(.medium)
        }
        
        gameIc.layer.cornerRadius = 25
        gameIc.backgroundColor = .fill.withAlphaComponent(0.7)
        gameIc.clipsToBounds = true
        container.addSubview(gameIc)
        gameIc.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(50)
        }
        
        let gameView = buildGameView()
        container.addSubview(gameView)
        gameView.snp.makeConstraints { make in
            make.leading.equalTo(gameIc.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
    }
    
    private func buildGameView() -> UIView {
        gameView.isHidden = true
        
        let tapView = UIView()
        tapView.onTap { [weak self] in
            guard let self else { return }
            guard let curItem else { return }
            delegate?.onIMChatGameMateSkillCell(cell: self, didClickOrder: curItem)
        }
        gameView.addSubview(tapView)
        tapView.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.width.equalTo(100)
        }
        
        let order = UIButton()
        order.setBackgroundImage(.primary_8, for: .normal)
        order.layer.cornerRadius = 12
        order.clipsToBounds = true
        order.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curItem else { return }
            delegate?.onIMChatGameMateSkillCell(cell: self, didClickOrder: curItem)
        }), for: .touchUpInside)
        gameView.addSubview(order)
        order.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
            make.height.equalTo(24)
        }
        
        let orderTitle = UILabel()
        orderTitle.text = .init(key: "A00041")
        orderTitle.font = .heading_h5
        orderTitle.textColor = .text_1
        orderTitle.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        orderTitle.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        order.addSubview(orderTitle)
        orderTitle.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.horizontalEdges.equalToSuperview().inset(17)
        }
        
        let infoView = UIView()
        gameView.addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
            make.trailing.lessThanOrEqualTo(order.snp.leading).offset(-16)
        }
        
        gameNameLabel.font = .heading_h4
        gameNameLabel.textColor = .text_5
        gameNameLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
        gameNameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        infoView.addSubview(gameNameLabel)
        gameNameLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let priceView = UIStackView()
        priceView.spacing = 2
        infoView.addSubview(priceView)
        priceView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
            make.top.equalTo(gameNameLabel.snp.bottom).offset(1)
            make.bottom.equalToSuperview()
        }
        
        discountView.discountOnly = true
        priceView.addArrangedSubview(discountView)
        
        let coin = UIImageView.coinImageView()
        priceView.addArrangedSubview(coin)
        
        gamePriceLabel.font = .body_m
        gamePriceLabel.textColor = .text_5
        priceView.addArrangedSubview(gamePriceLabel)
        
        return gameView
    }
}

#if DEBUG

import SwiftUI

struct LNIMChatGameMateSkillCellPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNIMChatGameMateSkillCell()
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(72)
        }
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNIMChatGameMateSkillCellPreview()
})
#endif

