//
//  LNOrderDetailCardView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/23.
//

import Foundation
import UIKit
import SnapKit


enum LNOrderDetailCardViewType {
    case normal
    case small
}


class LNOrderDetailCardView: UIView {
    private let curType: LNOrderDetailCardViewType
    
    private let avatar = UIImageView()
    private let nameLabel = UILabel()
    
    private let gameLabel = UILabel()
    private let priceLabel = UILabel()
    private let countLabel = UILabel()
    private var extraView: UIView?
    private let extraLabel = UILabel()
    private let orderIdLabel = UILabel()
    private let timeLabel = UILabel()
    private let totalLabel = UILabel()
    private var discountView: UIView?
    private let discountLabel = UILabel()
    
    init(type: LNOrderDetailCardViewType = .normal) {
        curType = type
        super.init(frame: .zero)
        
        setupViews()
    }
    
    func update(_ item: LNOrderDetailResponse) {
        avatar.showAvatar(item.orderInfo.avatar)
        nameLabel.text = item.orderInfo.nickname
        gameLabel.text = item.orderInfo.bizCategoryName
        priceLabel.text = "\(item.orderInfo.price.toDisplay)"
        countLabel.text = "\(item.orderInfo.unit)x\(item.orderInfo.purchaseQty)"
        extraLabel.text = item.orderInfo.customerRemark
        extraView?.isHidden = item.orderInfo.customerRemark.isEmpty
        orderIdLabel.text = item.orderInfo.orderId
        timeLabel.text = (TimeInterval(item.orderInfo.createTime) / 1000.0).formattedFullDateWithTime()
        
        totalLabel.text = item.orderInfo.totalAmount.toDisplay
        discountView?.isHidden = item.orderInfo.totalAmount >= item.orderInfo.price * Double(item.orderInfo.purchaseQty)
        discountLabel.text = "\(item.orderInfo.totalAmount - item.orderInfo.price * Double(item.orderInfo.purchaseQty))"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderDetailCardView {
    private func setupViews() {
        layer.cornerRadius = 12
        backgroundColor = .fill
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = curType == .normal ? 2 : 6
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.verticalEdges.equalToSuperview().inset(curType == .normal ? 8 : 10)
        }
        
        stackView.addArrangedSubview(buildUserInfo())
        
        if curType == .normal {
            stackView.addArrangedSubview(buildLine())
        }
        
        gameLabel.font = .body_s
        gameLabel.textColor = .text_5
        let gameInfo = buildDetailInfo(title: .init(key: "A00131"), contentView: gameLabel)
        stackView.addArrangedSubview(gameInfo)
        
        let priceView = buildPrice()
        stackView.addArrangedSubview(priceView)
        
        countLabel.font = curType == .normal ? .body_s : .body_xs
        countLabel.textColor = .text_5
        let countView = buildDetailInfo(title: .init(key: "A00122"), contentView: countLabel)
        stackView.addArrangedSubview(countView)
        
        let discountView = buildDiscount()
        stackView.addArrangedSubview(discountView)
        self.discountView = discountView
        
        extraLabel.font = curType == .normal ? .body_s : .body_xs
        extraLabel.textColor = .text_5
        extraLabel.numberOfLines = 0
        extraLabel.textAlignment = .right
        let extraView = buildDetailInfo(title: .init(key: "A00086"), contentView: extraLabel)
        stackView.addArrangedSubview(extraView)
        self.extraView = extraView
        
        orderIdLabel.font = curType == .normal ? .body_s : .body_xs
        orderIdLabel.textColor = .text_5
        let orderView = buildDetailInfo(title: .init(key: "A00132"), contentView: orderIdLabel)
        stackView.addArrangedSubview(orderView)
        
        timeLabel.font = curType == .normal ? .body_s : .body_xs
        timeLabel.textColor = .text_5
        let timeView = buildDetailInfo(title: .init(key: "A00133"), contentView: timeLabel)
        
        stackView.addArrangedSubview(timeView)
        stackView.addArrangedSubview(buildLine())
        stackView.addArrangedSubview(buildTotal())
    }
    
    private func buildUserInfo() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(curType == .normal ? 48 : 30)
        }
        
        avatar.layer.cornerRadius = curType == .normal ? 20 : 13
        avatar.clipsToBounds = true
        container.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(12)
            make.width.height.equalTo(curType == .normal ? 40 : 26)
        }
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_5
        container.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(avatar.snp.trailing).offset(8)
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        return container
    }
    
    private func buildLine() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(curType == .normal ? 20 : 0.5)
        }
        
        let line = UIView()
        line.backgroundColor = .fill_2
        container.addSubview(line)
        line.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(10)
            make.centerY.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        return container
    }
    
    private func buildDetailInfo(title: String, contentView: UIView) -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(curType == .normal ? 30 : 18).priority(.medium)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = curType == .normal ? .body_s : .body_xs
        titleLabel.textColor = .text_5
        titleLabel.text = title
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(12)
            make.top.equalToSuperview().offset(curType == .normal ? 8 : 2)
        }
        
        container.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-12)
            make.leading.greaterThanOrEqualTo(titleLabel.snp.trailing).offset(24)
            make.top.greaterThanOrEqualToSuperview().offset(curType == .normal ? 8 : 2)
        }
        
        return container
    }
    
    private func buildPrice() -> UIView {
        let priceView = UIView()
        
        let coin = UIImageView.coinImageView()
        priceView.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.leading.centerY.equalToSuperview()
            make.width.height.equalTo(14)
        }
        
        priceLabel.font = .body_s
        priceLabel.textColor = .text_5
        priceView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(coin.snp.trailing)
        }
        
        return buildDetailInfo(title: .init(key: "A00128"), contentView: priceView)
    }
    
    private func buildDiscount() -> UIView {
        let discountView = UIView()
        
        let coin = UIImageView.coinImageView()
        discountView.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.leading.centerY.equalToSuperview()
            make.width.height.equalTo(14)
        }
        
        discountLabel.font = .body_s
        discountLabel.textColor = .text_5
        discountView.addSubview(discountLabel)
        discountLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(coin.snp.trailing)
        }
        
        return buildDetailInfo(title: .init(key: "A00129"), contentView: discountView)
    }
    
    private func buildTotal() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(38)
        }
        
        totalLabel.font = .heading_h3
        totalLabel.textColor = .text_5
        container.addSubview(totalLabel)
        totalLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-12)
        }
        
        let coin = UIImageView.coinImageView()
        container.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(totalLabel.snp.leading)
            make.width.height.equalTo(18)
        }
        
        let text = UILabel()
        text.font = .body_s
        text.textColor = .text_5
        text.text = .init(key: "A00124")
        container.addSubview(text)
        text.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(coin.snp.leading)
        }
        
        return container
    }
}
