//
//  KMOpenDMGPopupBootWC.swift
//  PDF Reader Pro
//
//  Created by wanjun on 2024/11/6.
//  打开DMG时的弹窗引导
//

import Cocoa

// 7天免费试用 高级版
let KMAdvancedAnnualTrailKey                = "KMAdvancedAnnualTrailKey"

class KMOpenDMGPopupBootWC: NSWindowController {
    
    @IBOutlet weak var titleLabel: NSTextField!
    @IBOutlet weak var subTitleLabel: NSTextField!
    @IBOutlet weak var freeBox: KMBox!
    @IBOutlet weak var freeLabel: NSTextField!
    @IBOutlet weak var signInTextView: NSTextView!
    
    private var viewModel = KMOpenDMGPopBootModel()
    
    static let shared: KMOpenDMGPopupBootWC = {
        let windowC = KMOpenDMGPopupBootWC(windowNibName: "KMOpenDMGPopupBootWC")
        return windowC
    }()

    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
        
        languageLocalized()
        initializeUI()
        NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
    }
    
    @objc func changeEffectiveAppearance() {
        self.initializeUI()
    }

    // MARK: Private Method
    
    private func languageLocalized() -> Void {
        titleLabel.stringValue = NSLocalizedString("Free to Get 7-Day VIP Now", tableName: "MemberCenterLocalizable", comment: "")
        subTitleLabel.stringValue = String(format: " · %@", NSLocalizedString("Use all functions without restrictions", tableName: "MemberCenterLocalizable", comment: ""))
        freeLabel.stringValue = NSLocalizedString("Get 7-Day VIP Free", tableName: "MemberCenterLocalizable", comment: "")
    }
    
    private func initializeUI() -> Void {
        let isDarkModel = KMAdvertisementConfig.isDarkModel()
        if  isDarkModel {
            self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "0E1114").cgColor;
        } else {
            self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "FFFFFF").cgColor;
        }

        titleLabel.textColor = NSColor(named: "4982E6")
        titleLabel.font = NSFont.SFProTextSemiboldFont(24)
        subTitleLabel.textColor = NSColor(named: "101828")
        subTitleLabel.font = NSFont.SFProTextRegularFont(14)
        freeBox.fillColor = NSColor(named: "273C62") ?? .blue
        freeLabel.textColor = NSColor(named: "FFFFFF")
        freeLabel.font = NSFont.SFProTextRegularFont(13)
        signInTextView.isEditable = false
        signInTextView.isSelectable = true
        signInTextView.textColor = NSColor.black
        signInTextView.font = NSFont.SFProTextRegularFont(12)

        if let _ =  signInTextView.superview {
            signInTextView.km_add_left_constraint()
            signInTextView.km_add_top_constraint()
            signInTextView.km_add_trailing_constraint()
            signInTextView.km_add_height_constraint(constant: 30)
        }
        let tipsString = NSLocalizedString("Already have an account? %@", tableName: "MemberCenterLocalizable", comment: "")
        let specialOffer = NSLocalizedString("Sign in", tableName: "MemberCenterLocalizable", comment: "")
        let fullString = String(format: tipsString, specialOffer)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        
        let attributedString = NSMutableAttributedString(string: fullString)
        let specialOfferRange = (fullString as NSString).range(of: specialOffer)
        let linkColor = NSColor(named: "4982E6") ?? NSColor.blue
        let font = NSFont.SFProTextRegularFont(12)
        attributedString.addAttributes([
            .foregroundColor: NSColor(named: "42464D") ?? NSColor.black as Any,
            .font: font,
            .paragraphStyle: paragraphStyle
        ], range: (fullString as NSString).range(of: fullString))
        attributedString.addAttributes([
            .foregroundColor: linkColor,
            .link: "action://openDetail",
            .font: font
        ], range: specialOfferRange)
        signInTextView.textStorage?.setAttributedString(attributedString)
        signInTextView.delegate = self
        
        freeBox.moveCallback =  { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
            guard let self = self else { return }
        }
        freeBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
            guard let self = self else { return }
            if downEntered {
                // 存储标识
                KMDataManager.ud_set(true, forKey: KMAdvancedAnnualTrailKey)
                
                self.viewModel.getVipFree()
                self.window?.close()
            }
        }

    }
}

extension KMOpenDMGPopupBootWC: NSTextViewDelegate {
    
    func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool {
        if let urlString = link as? String, urlString == "action://openDetail" {
            viewModel.signUpAction()
            self.window?.close()
            return true
        }
        return false
    }
}