//
//  LNJoinUsAvatarExamplePanel.swift
//  Gami
//
//  Created by OneeChan on 2026/1/20.
//

import Foundation
import UIKit
import SnapKit


class LNJoinUsAvatarExamplePanel: LNPopupView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNJoinUsAvatarExamplePanel {
    private func setupViews() {
        let header = buildHeader()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let goodExample = buildGoodExample()
        container.addSubview(goodExample)
        goodExample.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(header.snp.bottom).offset(3)
            make.leading.greaterThanOrEqualToSuperview().offset(16)
        }
        
        let errExample = buildErr()
        container.addSubview(errExample)
        errExample.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(goodExample.snp.bottom).offset(20)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
    }
    
    private func buildHeader() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(50)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "B00047")
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let closeButton = UIButton()
        closeButton.setImage(.init(systemName: "xmark")?.withRenderingMode(.alwaysTemplate), for: .normal)
        closeButton.tintColor = .text_2
        closeButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            dismiss()
        }), for: .touchUpInside)
        container.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-12)
            make.width.height.equalTo(24)
        }
        
        return container
    }
    
    private func buildGoodExample() -> UIView {
        let container = UIView()
        
        let avatar = UIImageView()
        avatar.image = .icAvatarExampleCorrect
        avatar.layer.cornerRadius = 12
        avatar.clipsToBounds = true
        container.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.width.height.equalTo(100)
        }
        
        let checkIc = UIImageView()
        checkIc.image = .icCheck26
        container.addSubview(checkIc)
        checkIc.snp.makeConstraints { make in
            make.trailing.equalTo(avatar).offset(2)
            make.bottom.equalTo(avatar).offset(2)
            make.bottom.equalToSuperview()
        }
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 9
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.equalTo(avatar.snp.trailing).offset(18)
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        let text1 = buildExampleText(.init(key: "B00037"))
        stackView.addArrangedSubview(text1)
        
        let text2 = buildExampleText(.init(key: "B00048"))
        stackView.addArrangedSubview(text2)
        
        let text3 = buildExampleText(.init(key: "B00049"))
        stackView.addArrangedSubview(text3)
        
        return container
    }
    
    private func buildExampleText(_ text: String) -> UIView {
        let container = UIView()
        
        let dot = UIView()
        dot.backgroundColor = .primary_3
        dot.layer.cornerRadius = 3
        container.addSubview(dot)
        dot.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalToSuperview().offset(5)
            make.width.height.equalTo(6)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .body_m
        titleLabel.textColor = .text_5
        titleLabel.text = text
        titleLabel.numberOfLines = 0
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(dot.snp.trailing).offset(7)
        }
        
        return container
    }
    
    private func buildErr() -> UIView {
        let container = UIStackView()
        container.axis = .vertical
        container.spacing = 12
        
        let line1 = UIStackView()
        line1.spacing = 13
        line1.distribution = .fillEqually
        line1.alignment = .top
        line1.addArrangedSubview(buildErrExample(.icAvatarExample1, text: .init(key: "B00050")))
        line1.addArrangedSubview(buildErrExample(.icAvatarExample2, text: .init(key: "B00051")))
        line1.addArrangedSubview(buildErrExample(.icAvatarExample3, text: .init(key: "B00052")))
        line1.addArrangedSubview(buildErrExample(.icAvatarExample4, text: .init(key: "B00053")))
        container.addArrangedSubview(line1)
        
        let line2 = UIStackView()
        line2.spacing = 13
        line2.distribution = .fillEqually
        line2.alignment = .top
        line2.addArrangedSubview(buildErrExample(.icAvatarExample5, text: .init(key: "B00054")))
        line2.addArrangedSubview(buildErrExample(.icAvatarExample6, text: .init(key: "B00055")))
        line2.addArrangedSubview(buildErrExample(.icAvatarExample7, text: .init(key: "B00056")))
        line2.addArrangedSubview(buildErrExample(.icAvatarExample8, text: .init(key: "B00057")))
        container.addArrangedSubview(line2)
        
        return container
    }
    
    private func buildErrExample(_ icon: UIImage, text: String) -> UIView {
        let container = UIView()
        
        let avatar = UIImageView()
        avatar.image = icon
        container.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(avatar.snp.width)
        }
        
        let xmark = UIImageView()
        xmark.image = .icXMark
        container.addSubview(xmark)
        xmark.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(avatar.snp.bottom)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .body_xs
        titleLabel.textColor = .text_5
        titleLabel.text = text
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 2
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(xmark.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        return container
    }
}
