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

import Foundation
import UIKit
import SnapKit


class LNMineQRCodeShareView: 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 LNMineQRCodeShareView: LNProfileManagerNotify {
    func onUserInfoChanged(userInfo: LNUserProfileVO) {
        guard userInfo.userNo.isMyUid else { return }
        isHidden = !userInfo.playmate
    }
}

extension LNMineQRCodeShareView {
    private func setupViews() {
        backgroundColor = .fill
        layer.cornerRadius = 18
        layer.borderWidth = 1
        layer.borderColor = UIColor.fill.cgColor
        clipsToBounds = true
        snp.makeConstraints { make in
            make.height.equalTo(36)
        }
        
        let background = UIImageView()
        background.image = .primary_6
        background.isUserInteractionEnabled = false
        addSubview(background)
        background.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(1)
        }
        
        let qrCode = UIImageView()
        qrCode.image = .icQrGreen
        addSubview(qrCode)
        qrCode.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 14)
        arrow.tintColor = .text_3
        addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00212")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_4
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(qrCode.snp.trailing).offset(4)
            make.trailing.lessThanOrEqualTo(arrow.snp.leading).offset(-16)
        }
        
        onTap {
            let panel = LNOrderGenerateQRCodePanel()
            panel.popup()
        }
    }
}
