//
//  LNMineOrderRecordView.swift
//  Gami
//
//  Created by OneeChan on 2026/1/22.
//

import Foundation
import UIKit
import SnapKit


class LNMineOrderRecordView: UIView {
    private let incomeLabel = UILabel()
    private let exposureLabel = UILabel()
    private let visitorLabel = UILabel()
    
    private let statusButton = UIButton()
    private let statusLabel = UILabel()
    private let statusDescLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
        
        onGameMateManagerInfoChanged()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNMineOrderRecordView: LNGameMateManagerNotify {
    func onGameMateManagerInfoChanged() {
        guard let myGameMateInfo else { return }
        
        incomeLabel.text = myGameMateInfo.weekBeanIncome.toDisplay
        exposureLabel.text = "\(myGameMateInfo.exposureCountDay)"
        visitorLabel.text = "\(myGameMateInfo.potentialUserCount)"
        
        if myGameMateInfo.playmateOpen {
            statusButton.setBackgroundImage(.primary_7, for: .normal)
            statusLabel.text = .init(key: "B00087")
            statusDescLabel.text = .init(key: "B00088")
        } else {
            statusButton.setBackgroundImage(.primary_8, for: .normal)
            statusLabel.text = .init(key: "B00089")
            statusDescLabel.text = .init(key: "B00090")
        }
    }
}

extension LNMineOrderRecordView {
    private func setupViews() {
        layer.cornerRadius = 12
        backgroundColor = .fill
        onTap { [weak self] in
            guard let self else { return }
            pushToMateCenter()
        }
        
        let header = buildHeaderView()
        addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let recordView = buildRecordInfo()
        addSubview(recordView)
        recordView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(24)
            make.top.equalTo(header.snp.bottom)
        }
        
        let openButton = buildOpenButton()
        addSubview(openButton)
        openButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(recordView.snp.bottom).offset(12)
            make.bottom.equalToSuperview().offset(-12)
        }
    }
    
    private func buildHeaderView() -> UIView {
        let container = UIView()
        container.isUserInteractionEnabled = false
        container.snp.makeConstraints { make in
            make.height.equalTo(40)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "B00071")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
        }
        
        let arrow = UIImageView.arrowImageView(size: 16)
        arrow.tintColor = .text_4
        container.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        return container
    }
    
    private func buildRecordInfo() -> UIView {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.spacing = 28
        stackView.alignment = .center
        stackView.isUserInteractionEnabled = false
        stackView.snp.makeConstraints { make in
            make.height.equalTo(56)
        }
        
        stackView.addArrangedSubview(buildInCome())
        stackView.addArrangedSubview(buildLine())
        stackView.addArrangedSubview(buildDataItemView(.init(key: "B00073"), contentLabel: exposureLabel))
        stackView.addArrangedSubview(buildDataItemView(.init(key: "B00116"), contentLabel: visitorLabel))
        
        return stackView
    }
    
    private func buildInCome() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.width.greaterThanOrEqualTo(120)
        }
        
        let beanView = UIView()
        container.addSubview(beanView)
        beanView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let beanIc = UIImageView.beanImageView()
        beanView.addSubview(beanIc)
        beanIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview()
            make.width.height.equalTo(20)
        }
        
        incomeLabel.text = "0"
        incomeLabel.font = .heading_h1
        incomeLabel.textColor = .text_5
        beanView.addSubview(incomeLabel)
        incomeLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(beanIc.snp.trailing)
        }
        
        let desclabel = UILabel()
        desclabel.text = .init(key: "B00072")
        desclabel.font = .body_s
        desclabel.textColor = .text_5
        desclabel.textAlignment = .center
        desclabel.numberOfLines = 2
        container.addSubview(desclabel)
        desclabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(beanView.snp.bottom).offset(6)
        }
        
        return container
    }
    
    private func buildDataItemView(_ title: String, contentLabel: UILabel) -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.width.lessThanOrEqualTo(60)
        }
        
        contentLabel.text = "0"
        contentLabel.font = .heading_h2
        contentLabel.textColor = .text_5
        container.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        let descLabel = UILabel()
        descLabel.text = title
        descLabel.font = .body_s
        descLabel.textColor = .text_4
        descLabel.numberOfLines = 2
        descLabel.textAlignment = .center
        container.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(contentLabel.snp.bottom).offset(2)
        }
        
        return container
    }
    
    private func buildOpenButton() -> UIView {
        statusButton.layer.cornerRadius = 26
        statusButton.clipsToBounds = true
        statusButton.setBackgroundImage(.primary_8, for: .normal)
        statusButton.addAction(UIAction(handler: { _ in
            showLoading()
            let isOpen = myGameMateInfo?.playmateOpen == true
            LNGameMateManager.shared.enableGameMate(open: !isOpen) { success in
                dismissLoading()
            }
        }), for: .touchUpInside)
        statusButton.snp.makeConstraints { make in
            make.height.equalTo(56)
        }
        
        let container = UIView()
        container.isUserInteractionEnabled = false
        statusButton.addSubview(container)
        container.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview().offset(16)
        }
        
        statusLabel.font = .heading_h3
        statusLabel.textColor = .text_1
        statusLabel.textAlignment = .center
        container.addSubview(statusLabel)
        statusLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        statusDescLabel.font = .body_xs
        statusDescLabel.textColor = .primary_1
        container.addSubview(statusDescLabel)
        statusDescLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(statusLabel.snp.bottom)
        }
        
        return statusButton
    }
    
    private func buildLine() -> UIView {
        let line = UIView()
        line.backgroundColor = .init(hex: "#D9D9D9")
        line.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.height.equalTo(37)
        }
    
        return line
    }
}
