//
//  PermissionRequiredDialogViewController.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 首页无业务权限时展示的 Android 风格强制提示弹窗。
@MainActor
final class PermissionRequiredDialogViewController: UIViewController {

    private let onExitApp: () -> Void
    private let onGoToApply: () -> Void
    private let dimView = UIView()
    private let cardView = UIView()

    /// 创建不可点击遮罩关闭的权限提示弹窗。
    init(onExitApp: @escaping () -> Void, onGoToApply: @escaping () -> Void) {
        self.onExitApp = onExitApp
        self.onGoToApply = onGoToApply
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .overFullScreen
        modalTransitionStyle = .crossDissolve
        isModalInPresentation = true
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
        dimView.backgroundColor = UIColor.black.withAlphaComponent(0.32)
        cardView.backgroundColor = .white
        cardView.layer.cornerRadius = 16

        let titleLabel = UILabel()
        titleLabel.text = "提示"
        titleLabel.font = .systemFont(ofSize: 18, weight: .bold)
        titleLabel.textColor = UIColor(hex: 0x333333)
        titleLabel.textAlignment = .center

        let messageLabel = UILabel()
        messageLabel.text = "您还没有权限，请先申请权限"
        messageLabel.font = .systemFont(ofSize: 16)
        messageLabel.textColor = UIColor(hex: 0x4B5563)
        messageLabel.textAlignment = .center
        messageLabel.numberOfLines = 0

        let exitButton = makeButton(
            title: "退出app",
            titleColor: UIColor(hex: 0x333333),
            backgroundColor: .white,
            borderColor: UIColor(hex: 0xE5E5E5)
        )
        exitButton.addTarget(self, action: #selector(exitTapped), for: .touchUpInside)

        let applyButton = makeButton(
            title: "去申请",
            titleColor: .white,
            backgroundColor: AppColor.primary,
            borderColor: nil
        )
        applyButton.addTarget(self, action: #selector(applyTapped), for: .touchUpInside)

        let buttons = UIStackView(arrangedSubviews: [exitButton, applyButton])
        buttons.axis = .horizontal
        buttons.spacing = 12
        buttons.distribution = .fillEqually

        let stack = UIStackView(arrangedSubviews: [titleLabel, messageLabel, buttons])
        stack.axis = .vertical
        stack.alignment = .fill
        stack.spacing = 16
        stack.setCustomSpacing(24, after: messageLabel)

        view.addSubview(dimView)
        view.addSubview(cardView)
        cardView.addSubview(stack)

        dimView.snp.makeConstraints { make in make.edges.equalToSuperview() }
        cardView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(32)
        }
        stack.snp.makeConstraints { make in make.edges.equalToSuperview().inset(24) }
        buttons.snp.makeConstraints { make in make.height.equalTo(44) }
    }

    private func makeButton(
        title: String,
        titleColor: UIColor,
        backgroundColor: UIColor,
        borderColor: UIColor?
    ) -> UIButton {
        let button = UIButton(type: .system)
        button.setTitle(title, for: .normal)
        button.setTitleColor(titleColor, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        button.backgroundColor = backgroundColor
        button.layer.cornerRadius = 8
        button.layer.borderWidth = borderColor == nil ? 0 : 1
        button.layer.borderColor = borderColor?.cgColor
        button.accessibilityLabel = title
        return button
    }

    @objc private func exitTapped() {
        onExitApp()
    }

    @objc private func applyTapped() {
        dismiss(animated: true) { [onGoToApply] in onGoToApply() }
    }
}
