//
//  LNRoomGiftBottomView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/23.
//

import Foundation
import UIKit
import SnapKit


protocol LNRoomGiftBottomViewDelegate: AnyObject {
    func onRoomGiftBottomViewDidTapBalance(_ view: LNRoomGiftBottomView)
    func onRoomGiftBottomViewDidTapSend(_ view: LNRoomGiftBottomView)
}


class LNRoomGiftBottomView: UIView {
    private let balanceLabel = UILabel()
    private let sendButton = UIButton(type: .system)
    private let countView = UIView()
    private let minusButton = UIButton()
    private let countLabel = UILabel()
    private var countLabelWidth: Constraint?
    private(set) var curCount: Int = 0 {
        didSet {
            countLabel.text = "\(curCount)"
            minusButton.isEnabled = curCount > 1
            minusButton.alpha = curCount > 1 ? 1.0 : 0.4
            if curCount > 99 {
                countLabelWidth?.update(offset: 35)
            } else if curCount > 9 {
                countLabelWidth?.update(offset: 25)
            } else {
                countLabelWidth?.update(offset: 15)
            }
        }
    }
    var enable: Bool = false {
        didSet {
            sendButton.isEnabled = enable
            sendButton.alpha = enable ? 1.0 : 0.4
            countView.isHidden = !enable
            if !enable {
                curCount = 1
            }
        }
    }
    
    weak var delegate: LNRoomGiftBottomViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        LNEventDeliver.addObserver(self)
        
        DispatchQueue.main.async { [weak self] in
            guard let self else { return }
            curCount = 1
            enable = false
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNRoomGiftBottomView: LNPurchaseManagerNotify {
    func onUserWalletInfoChanged(info: LNUserWalletInfo) {
        balanceLabel.text = myWalletInfo.diamond.toDisplay
    }
}

private extension LNRoomGiftBottomView {
    func setupViews() {
        snp.makeConstraints { make in
            make.height.equalTo(54)
        }
        
        let balanceView = UIView()
        balanceView.onTap { [weak self] in
            guard let self else { return }
            delegate?.onRoomGiftBottomViewDidTapBalance(self)
        }
        addSubview(balanceView)
        balanceView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.verticalEdges.equalToSuperview()
        }
        
        let diamond = UIImageView.diamondImageView(true)
        balanceView.addSubview(diamond)
        diamond.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview().offset(-1)
            make.width.height.equalTo(18)
        }
        
        balanceLabel.font = .heading_h3
        balanceLabel.textColor = .text_1
        balanceLabel.text = myWalletInfo.diamond.toDisplay
        balanceView.addSubview(balanceLabel)
        balanceLabel.snp.makeConstraints { make in
            make.leading.equalTo(diamond.snp.trailing).offset(2)
            make.centerY.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 12, weight: .semibold)
        arrow.tintColor = .text_1
        balanceView.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.leading.equalTo(balanceLabel.snp.trailing).offset(4)
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        countView.layer.cornerRadius = 15
        countView.backgroundColor = .fill.withAlphaComponent(0.1)
        addSubview(countView)
        countView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.trailing.equalToSuperview().offset(-10)
            make.height.equalTo(30)
        }
        
        sendButton.setTitle(.init(key: "A00305"), for: .normal)
        sendButton.setTitleColor(.text_1, for: .normal)
        sendButton.titleLabel?.font = .heading_h5
        sendButton.layer.cornerRadius = 15
        sendButton.clipsToBounds = true
        sendButton.setBackgroundImage(.primary_8, for: .normal)
        sendButton.contentEdgeInsets = .init(top: 0, left: 12, bottom: 0, right: 12)
        sendButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            delegate?.onRoomGiftBottomViewDidTapSend(self)
        }), for: .touchUpInside)
        addSubview(sendButton)
        sendButton.snp.makeConstraints { make in
            make.trailing.equalTo(countView)
            make.centerY.equalTo(countView)
            make.height.equalTo(countView)
        }
        
        let config = UIImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
        minusButton.setImage(.init(systemName: "minus", withConfiguration: config), for: .normal)
        minusButton.backgroundColor = .fill.withAlphaComponent(0.6)
        minusButton.layer.cornerRadius = 12
        minusButton.tintColor = .fill_7
        minusButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            curCount -= 1
        }), for: .touchUpInside)
        countView.addSubview(minusButton)
        minusButton.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(3)
        }
        
        countLabel.font = .body_m
        countLabel.textColor = .text_1
        countLabel.textAlignment = .center
        countView.addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(minusButton.snp.trailing).offset(6)
            countLabelWidth = make.width.equalTo(15).constraint
        }
        
        let fakeView = UIView()
        countView.addSubview(fakeView)
        fakeView.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.width.equalTo(sendButton)
        }
        
        let addButton = UIButton()
        addButton.setImage(.init(systemName: "plus", withConfiguration: config), for: .normal)
        addButton.backgroundColor = .fill.withAlphaComponent(0.6)
        addButton.layer.cornerRadius = 12
        addButton.tintColor = .fill_7
        addButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            curCount += 1
        }), for: .touchUpInside)
        countView.addSubview(addButton)
        addButton.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.leading.equalTo(countLabel.snp.trailing).offset(6)
            make.trailing.equalTo(fakeView.snp.leading).offset(-12)
        }
    }
}
