//
//  LNMineFunctionView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/19.
//

import Foundation
import UIKit
import SnapKit


class LNMineFunctionView: UIView {
    private let stackView = UIStackView()
    private var joinView: UIView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
        
        onUserInfoChanged(userInfo: myUserInfo)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNMineFunctionView: LNProfileManagerNotify {
    func onUserInfoChanged(userInfo: LNUserProfileVO) {
        guard userInfo.userNo.isMyUid else { return }
        joinView?.isHidden = userInfo.playmate
        if !userInfo.playmate {
            stackView.distribution = .equalCentering
        } else {
            stackView.distribution = .fillEqually
        }
    }
}

extension LNMineFunctionView {
    private func setupViews() {
        layer.cornerRadius = 12
        backgroundColor = .fill
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00207")
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(12)
        }
        
        stackView.axis = .horizontal
        stackView.distribution = .equalCentering
        stackView.alignment = .top
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(22)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        let order = buildFunctionItemView(icon: .icProfileOrder, title: .init(key: "A00208"))
        order.onTap { [weak self] in
            guard let self else { return }
            pushToOrderList()
        }
        stackView.addArrangedSubview(order)
        
        let joinUs = buildFunctionItemView(icon: .icProfileJoinUs, title: .init(key: "B00063"))
        joinUs.onTap { [weak self] in
            guard let self else { return }
            
            showLoading()
            LNGameMateManager.shared.getJoinGameMateInfo { [weak self] res in
                dismissLoading()
                guard let self else { return }
                guard let res else { return }
                
                if res.step3Complete || res.underReview {
                    pushToJoinUsReview(true)
                    return
                }
                let config = LNJumpWebViewConfig(url: .joinUsUrl)
                pushToWebView(config)
            }
        }
        stackView.addArrangedSubview(joinUs)
        joinView = joinUs
        
        let help = buildFunctionItemView(icon: .icProfileHelp, title: .init(key: "A00210"))
        help.onTap { [weak self] in
            guard let self else { return }
            pushToHelpCenter()
        }
        stackView.addArrangedSubview(help)
        
        let settings = buildFunctionItemView(icon: .icProfileSettings, title: .init(key: "A00211"))
        settings.onTap { [weak self] in
            guard let self else { return }
            pushToSettings()
        }
        stackView.addArrangedSubview(settings)
    }
    
    private func buildFunctionItemView(icon: UIImage, title: String) -> UIView {
        let container = UIView()
        
        let imageView = UIImageView()
        imageView.isUserInteractionEnabled = false
        imageView.image = icon
        container.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.font = .body_s
        titleLabel.textColor = .text_4
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).offset(8)
            make.bottom.equalToSuperview()
        }
        
        return container
    }
}
