//
//  LNIMNotificationPermissionView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/30.
//

import Foundation
import UIKit
import SnapKit


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

extension LNIMNotificationPermissionView {
    private func requestPermission() {
        LNPermissionHelper.requestNotificationAuthorization(options: [.alert, .sound, .badge])
        { [weak self] granted, _ in
            guard let self else { return }
            guard granted else { return }
            UIApplication.shared.registerForRemoteNotifications()
            checkPermission()
        }
    }
    
    private func checkPermission() {
        LNPermissionHelper.fetchNotificationSettings { [weak self] settings in
            guard let self else { return }
            switch settings.authorizationStatus {
            case .authorized, .provisional, .ephemeral:
                isHidden = true
                break
            case .notDetermined:
                requestPermission()
                isHidden = false
            default:
                isHidden = false
                break
            }
        }
    }
    
    private func openNotificationSettings() {
        let settingsURL: URL? =
        if #available(iOS 16.0, *) {
            URL(string: UIApplication.openNotificationSettingsURLString)
        } else {
            URL(string: UIApplication.openSettingsURLString)
        }
        guard let settingsURL else { return }
        guard UIApplication.shared.canOpenURL(settingsURL) else { return }
        
        UIApplication.shared.open(settingsURL) { _ in }
    }
}

extension LNIMNotificationPermissionView {
    private func setupViews() {
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 12
        addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.verticalEdges.equalToSuperview()
            make.height.equalTo(50)
        }
        
        let close = UIButton()
        close.setImage(.init(systemName: "xmark")?.withRenderingMode(.alwaysTemplate), for: .normal)
        close.tintColor = .text_2
        close.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            isHidden = true
        }), for: .touchUpInside)
        container.addSubview(close)
        close.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
        }
        
        let open = UIButton()
        open.setTitle(.init(key: "A00095"), for: .normal)
        open.titleLabel?.font = .body_s
        open.setTitleColor(.text_1, for: .normal)
        open.setBackgroundImage(.primary_7, for: .normal)
        open.contentEdgeInsets = .init(top: 5, left: 10, bottom: 5, right: 10)
        open.clipsToBounds = true
        open.layer.cornerRadius = 12
        open.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            openNotificationSettings()
        }), for: .touchUpInside)
        container.addSubview(open)
        open.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
            make.height.equalTo(24)
        }
        
        let textView = UIView()
        container.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(close.snp.trailing).offset(12)
            make.trailing.lessThanOrEqualTo(open.snp.leading).offset(-12)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00096")
        titleLabel.font = .heading_h5
        titleLabel.textColor = .text_5
        textView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
        }
        
        let textLabel = UILabel()
        textLabel.text = .init(key: "A00097")
        textLabel.font = .body_xs
        textLabel.textColor = .text_5
        textView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
        }
    }
}
