//
//  LNOrderCustomView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/27.
//

import Foundation
import UIKit
import SnapKit


class LNOrderCustomView: UIView {
    private let showView = LNOrderQRCodeShowView()
    
    private let editView = UIView()
    private let priceLabel = UILabel()
    
    private let minusButton = UIButton()
    private let unitLabel = UILabel()
    private let countLabel = UILabel()
    private let addButton = UIButton()
    
    private let costLabel = UILabel()
    private let costUnitLabel = UILabel()
    
    private var curSkill: LNGameMateSkillVO?
    
    private var customCount = 1 {
        didSet {
            countLabel.text = "\(customCount)"
            if customCount <= 1 {
                minusButton.isEnabled = false
            } else {
                minusButton.isEnabled = true
            }
            updateEditCost()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ skill: LNGameMateSkillVO) {
        guard curSkill?.id != skill.id else { return }
        curSkill = skill
        
        showView.isHidden = true
        editView.isHidden = false
        
        priceLabel.text = skill.price.toDisplay
        unitLabel.text = skill.unit
        customCount = 1
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderCustomView {
    private func updateEditCost() {
        guard let skill = curSkill else { return }
        let cost = skill.price * Double(customCount)
        
        costLabel.text = cost.toDisplay
//        costUnitLabel.text = if customCount <= 1 {
//            "/\(skill.unit)"
//        } else {
//            "/\(skill.unit)x\(customCount)"
//        }
    }
    
    private func setupViews() {
        showView.isHidden = true
        addSubview(showView)
        showView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let editView = buildEditView()
        addSubview(editView)
        editView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    private func buildEditView() -> UIView {
        editView.isHidden = false
        
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 16
        editView.addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(26)
        }
        
        let priceView = buildPriceView()
        container.addSubview(priceView)
        priceView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        let line = UIView()
        line.backgroundColor = .fill_4
        container.addSubview(line)
        line.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(priceView.snp.bottom).offset(6)
            make.height.equalTo(0.5)
        }
        
        let unit = buildEditUnitView()
        container.addSubview(unit)
        unit.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(line.snp.bottom).offset(6)
            make.bottom.equalToSuperview().offset(-12)
        }
        
        let cost = buildCostView()
        editView.addSubview(cost)
        cost.snp.makeConstraints { make in
            make.top.equalTo(container.snp.bottom).offset(16)
            make.trailing.equalToSuperview().offset(-16)
        }
        
        let generateButton = UIButton()
        generateButton.setTitle(.init(key: "A00155"), for: .normal)
        generateButton.titleLabel?.font = .heading_h3
        generateButton.setBackgroundImage(.primary_8, for: .normal)
        generateButton.layer.cornerRadius = 23.5
        generateButton.backgroundColor = .fill_4
        generateButton.clipsToBounds = true
        generateButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let skill = self.curSkill else { return }
            
            self.showView.update(skill, customCount: customCount)
            self.showView.isHidden = false
            self.editView.isHidden = true
        }), for: .touchUpInside)
        editView.addSubview(generateButton)
        generateButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().offset(-10)
            make.height.equalTo(47)
        }
        
        return editView
    }
    
    private func buildPriceView() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(45)
        }
        
        let coin = UIImageView.coinImageView()
        container.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
            make.width.height.equalTo(23)
        }
        
        priceLabel.font = .heading_h1
        priceLabel.textColor = .text_5
        container.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(coin.snp.trailing).offset(3)
            make.trailing.equalToSuperview().offset(-16)
            make.height.equalTo(22)
        }
        
        return container
    }
    
    private func buildEditUnitView() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(45)
        }
        
        addButton.setTitle("+", for: .normal)
        addButton.setTitleColor(.text_4, for: .normal)
        addButton.setTitleColor(.text_2, for: .disabled)
        addButton.backgroundColor = .primary_1
        addButton.layer.cornerRadius = 12
        addButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            self.customCount += 1
        }), for: .touchUpInside)
        container.addSubview(addButton)
        addButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-12)
            make.width.height.equalTo(24)
        }
        
        countLabel.text = "\(customCount)"
        countLabel.font = .body_m
        countLabel.textColor = .text_5
        container.addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(addButton.snp.leading).offset(-10)
        }
        
        minusButton.setTitle("-", for: .normal)
        minusButton.setTitleColor(.text_4, for: .normal)
        minusButton.setTitleColor(.text_2, for: .disabled)
        minusButton.backgroundColor = .primary_1
        minusButton.layer.cornerRadius = 12
        minusButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            self.customCount -= 1
        }), for: .touchUpInside)
        container.addSubview(minusButton)
        minusButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(countLabel.snp.leading).offset(-10)
            make.width.height.equalTo(24)
        }
        
        unitLabel.font = .body_m
        unitLabel.textColor = .text_5
        container.addSubview(unitLabel)
        unitLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
        }
        
        return container
    }
    
    private func buildCostView() -> UIView {
        let container = UIView()
        
        let coin = UIImageView.coinImageView()
        container.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        costLabel.font = .heading_h2
        costLabel.textColor = .text_5
        container.addSubview(costLabel)
        costLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalTo(coin.snp.trailing).offset(2)
        }
        
        costUnitLabel.font = .body_m
        costUnitLabel.textColor = .text_5
        container.addSubview(costUnitLabel)
        costUnitLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(costLabel.snp.trailing)
            make.trailing.equalToSuperview()
        }
        
        return container
    }
}
