//
//  QSLGuideusersToCommentManager.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/7/31.
//

import Foundation
import UIKit

enum QSLGuideusersToCommentType : Int{
    case member //会员
    case nonMember // 非会员
}
class QSLGuideusersToCommentManager: NSObject {
    
    static let commentShare = QSLGuideusersToCommentManager()

    var guideGobalModel : QSLGuideIsTriggeredModel = QSLGuideIsTriggeredModel()
    
    var commentType : QSLGuideusersToCommentType = .member
    
    //管理处理是否触发好评引导弹窗
    func manageWhetherTriggerPopUpWindow(_ showType : QSLGuideusersToCommentType) {
        //print("showTypesdfsdfs---\(showType)")
        QSLNetwork().request(.guideIsTriggered(dict: [String : Any]())) { response in
            //print("responsesfsdfsf-----\(response)")
            let guideIsModel : QSLGuideIsTriggeredModel = response.mapObject(QSLGuideIsTriggeredModel.self, modelKey: "data")
            self.commentType = showType
            self.guideGobalModel = guideIsModel
            if (!guideIsModel.triggerStatus && guideIsModel.isTrigger) {
                if  showType == .member {
                    DispatchQueue.main.async {
                        self.memberPositiveReviewPopWindow()
                    }
                } else {
                    DispatchQueue.main.async {
                        self.noMemberPositiveReviewPopWindow()
                    }
                }
            }
            
        } fail: { code, msg in
        }
    }
    
    ///领取好评引导奖励请求
    func requestForReceivingGoodReviewGuidance() {
        QSLNetwork().request(.guideReceiveReward(dict: [String : Any]())) { response in
            if self.commentType == .member {
                DispatchQueue.main.async {
                    self.popUpWindowForMemberReview()
                }
            } else {
                DispatchQueue.main.async {
                    self.popUpWindowForNonMemberReviews()
                }
            }
        } fail: { code, msg in
        }

    }
}


//管理各种类型的痰喘
extension QSLGuideusersToCommentManager {
    
    ///会员的好评弹窗
    @MainActor func memberPositiveReviewPopWindow() {
        // 创建深灰色文本样式
        let darkGrayAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]
        
        // 创建绿色文本样式
        let greenAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]
        
        let inputtr = NSMutableAttributedString()
        let titleOneAttr = NSAttributedString(string: "五星好评，", attributes: darkGrayAttributes)
        let titleTwoAttr = NSAttributedString(string: "多送\(self.guideGobalModel.extraMemberDays)天会员", attributes: greenAttributes)
        inputtr.append(titleOneAttr)
        inputtr.append(titleTwoAttr)
        
        if let currentWindow = UIApplication.keyWindow {
            QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "你的一条五星好评是我们前进的动力", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure:  { [weak self] in
                // 同步调用
                QSLInAppReviewmanager.requestReview()
                
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self?.requestForReceivingGoodReviewGuidance()
                }
            })
        }
    }
    
    ///非会员的好评弹窗
    @MainActor func noMemberPositiveReviewPopWindow() {
        // 创建深灰色文本样式
        let darkGrayAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]

        let inputtr = NSMutableAttributedString()
        let titleOneAttr = NSAttributedString(string: "给我们一个好评呗~", attributes: darkGrayAttributes)
        inputtr.append(titleOneAttr)
        
        if let currentWindow = UIApplication.keyWindow {
            QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "我们将回馈用户更多的优惠福利", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure:  { [weak self] in
                
                // 同步调用
                QSLInAppReviewmanager.requestReview()
                
                // 延迟1秒执行
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self?.requestForReceivingGoodReviewGuidance()
                }
                //self?.requestAddFri()
            })
        }
    }
    
    ///会员好评的结果弹窗
    @MainActor func popUpWindowForMemberReview() {
        // 创建深灰色文本样式
        let darkGrayAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]

        // 创建绿色文本样式
        let greenAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 28)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]

        let inputtr = NSMutableAttributedString()
        let titleOneAttr = NSAttributedString(string: "你已成功增加", attributes: darkGrayAttributes)
        let titleTwoAttr = NSAttributedString(string: " \(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes)
        let titleThreeAttr = NSAttributedString(string: "天会员", attributes: darkGrayAttributes)
        inputtr.append(titleOneAttr)
        inputtr.append(titleTwoAttr)
        inputtr.append(titleThreeAttr)
        
        
        if let currentWindow = UIApplication.keyWindow {
            QSLVipResultsAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "恭喜你，领取成功！", isOneBtn: true, oneBtnText: "我知道了", oneBtnClosure:  { [weak self] in
                //self?.requestAddFri()
            })
        }

    }
    
    ///非会员好评的结果弹窗
    @MainActor func popUpWindowForNonMemberReviews() {
        ///标题
        let titleDarkGrayAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]
        
        // 创建深灰色文本样式
        let darkGrayAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 16)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]
        
        
        // 创建绿色文本样式
        let greenAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0),
            .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black),
            .paragraphStyle: {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = .center
                return paragraphStyle
            }(),
            .kern: -1
        ]

        let inputtr = NSMutableAttributedString()
        let titleOneAttr = NSAttributedString(string: "感谢支持~", attributes: titleDarkGrayAttributes)
        inputtr.append(titleOneAttr)
        
        let contentAtter = NSMutableAttributedString()
        let contentAtterOne = NSAttributedString(string: "会员套餐体验", attributes: darkGrayAttributes)
        contentAtter.append(contentAtterOne)
        let contentAtterTwo = NSAttributedString(string: " \(self.guideGobalModel.returnDaysBasedOnRegistration())+\(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes)
        contentAtter.append(contentAtterTwo)
        let contentAtterTherr = NSAttributedString(string: "天", attributes: darkGrayAttributes)
        contentAtter.append(contentAtterTherr)
        
        if let currentWindow = UIApplication.keyWindow {
            QSLVipResultsNoMemberAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "已解锁会员套餐体验7+3福利套餐",bottomContentAttring: contentAtter, isOneBtn: true, oneBtnText: "我知道了", oneBtnClosure:  { [weak self] in
                
                //self?.requestAddFri()
            })
        }
    }
}
