//
//  QSLJumpManager.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/24.
//

import Foundation
import UIKit

enum JumpType: Int {
    case createInspration = 0   // 灵感创作
    case createDiy              // 自定义创作
    case mine                   // 我的
    case vip                    // 会员
    case login                  // 登录
    case accompaniment          // 伴奏
}

class QSLJumpManager: NSObject {
    
    static let shared = QSLJumpManager()
    
    private override init() {}
}

extension QSLJumpManager {

    // 跳转到Vip页面
    func pushToVip(type: QSLVipJumpType) {
        
        let vc = QSLVipController()
        vc.type = type
        self.rootViewController()?.pushVC(vc: vc)
        vc.finishHandler = {[weak self] isCancel, yearGood in
            if isCancel {
                return
            }
            if QSLBaseManager.shared.isVip() {
                return
            }
            if let currentWindow = UIApplication.keyWindow {
                QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_retention_show, eventProps: ["trigger_type":"close_vip_center"])
                QSLRetainPopUpAlertView.alert(view: currentWindow, isOneBtn: true, oneBtnText: "继续支付", oneBtnClosure:  { [weak self] in
                    QSLCountdownManager.shared.selectGood = yearGood
                    self?.unlockBtnAction()
                    QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_retention_click, eventProps: ["button":"continue_payment"])
                },closeBtnClosure: {
                    QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_retention_click, eventProps: ["button":"close"])
                })
            }
        }
    }
    
    func unlockBtnAction(){
        if(!QSLBaseManager.shared.isLogin()){
            if(QSLBaseManager.shared.isVip()){
                QSLJumpManager.shared.pushToLogin(type: .member)
                return
            }
            
            if(QSLConfig.loginPayEnable){
                QSLJumpManager.shared.pushToLogin(type: .member)
                return
            }
        }
        
        if let selectGood = QSLCountdownManager.shared.selectGood{
            
            let memberModel = QSLBaseManager.shared.userModel.memberModel
            
            if let subscriptionExpired = memberModel.subscriptionExpired, !subscriptionExpired {
                UIApplication.keyWindow?.toast(text: "你已经订阅了")
                return
            }
            
            QSLLoading.show()
            QSLVipManager.shared.startPay(goods: selectGood) { status, outTradeNo in
                QSLVipManager.shared.isPaying = false
                if status == .success {
                    QSLLoading.hide()
                    QSLLoading.success(text: "支付成功")
                    
                    //弹出是否好评的弹窗
                    //QSLGuideusersToCommentManager.commentShare.manageWhetherTriggerPopUpWindow(QSLGuideusersToCommentType.member)
                    
                    // 引力传递支付事件
                    gravityInstance?.trackPayEvent(withAmount: Int32(selectGood.amount), withPayType: "CNY", withOrderId: outTradeNo, withPayReason: selectGood.name, withPayMethod: "apple")
                    
                    QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_result, eventProps: ["is_member":QSLBaseManager.shared.isVip(),"purchase_result": "success","pay_amount":Int32(selectGood.amount)])
   
                    QSEventHandle.gravityPush(eventName: QSLGravityConst.vip_submit_success, eventProps: ["id": 01001])
                    
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        NotificationCenter.default.post(name: QSLNotification.QSLRefreshMember, object: nil)
                        
                        if(!QSLBaseManager.shared.isLogin()){
                            QSLJumpManager.shared.pushToLogin(type: .member)
                        }
                    }
                    
                
                } else if status == .cancel {
                    QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_result, eventProps: ["is_member":QSLBaseManager.shared.isVip(),"purchase_result": "cancel","pay_amount":Int32(selectGood.amount)])
                    QSLLoading.error(text: "支付取消")
                } else if status == .fail {
                    QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_result, eventProps: ["is_member":QSLBaseManager.shared.isVip(),"purchase_result": "fail","pay_amount":Int32(selectGood.amount)])
                    QSLLoading.error(text: "支付失败")
                } 
            }
        }
    }
    
    func unlockTrialBtnAction(){
        if(!QSLBaseManager.shared.isLogin()){
            if(QSLBaseManager.shared.isVip()){
                QSLJumpManager.shared.pushToLogin(type: .member)
                return
            }
            
            if(QSLConfig.loginPayEnable){
                QSLJumpManager.shared.pushToLogin(type: .member)
                return
            }
        }
        
        if let selectGood = QSLCountdownManager.shared.trialGood{
            
            let memberModel = QSLBaseManager.shared.userModel.memberModel
            
            if let subscriptionExpired = memberModel.subscriptionExpired, !subscriptionExpired {
                UIApplication.keyWindow?.toast(text: "你已经订阅了")
                return
            }
            
            QSLLoading.show()
            QSLVipManager.shared.startPay(goods: selectGood) { status, outTradeNo in
                QSLVipManager.shared.isPaying = false
                if status == .success {
                    QSLLoading.success(text: "支付成功")
                    
                    //弹出是否好评的弹窗
                    //QSLGuideusersToCommentManager.commentShare.manageWhetherTriggerPopUpWindow(QSLGuideusersToCommentType.member)
                    
                    // 引力传递支付事件
                    gravityInstance?.trackPayEvent(withAmount: Int32(selectGood.amount), withPayType: "CNY", withOrderId: outTradeNo, withPayReason: selectGood.name, withPayMethod: "apple")
                    
                    QSEventHandle.eventPush(eventName: QSLGravityConst.activity_purchase_result, eventProps: ["is_member":QSLBaseManager.shared.isVip(),"purchase_result": "success","pay_amount":Int32(selectGood.amount),"page_type":"coupon_popup"])
                    
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        NotificationCenter.default.post(name: QSLNotification.QSLRefreshMember, object: nil)
                        
                        if(!QSLBaseManager.shared.isLogin()){
                            QSLJumpManager.shared.pushToLogin(type: .member)
                        }
                    }
                    
                
                } else if status == .cancel {
                    QSEventHandle.eventPush(eventName: QSLGravityConst.activity_purchase_result, eventProps: ["is_member":QSLBaseManager.shared.isVip(),"purchase_result": "cancel","pay_amount":Int32(selectGood.amount),"page_type":"coupon_popup"])
                    QSLLoading.error(text: "支付取消")
                } else if status == .fail {
                    QSEventHandle.eventPush(eventName: QSLGravityConst.activity_purchase_result, eventProps: ["is_member":QSLBaseManager.shared.isVip(),"purchase_result": "fail","pay_amount":Int32(selectGood.amount),"page_type":"coupon_popup"])
                    QSLLoading.error(text: "支付失败")
                }
            }
        }
    }
    
    // 跳转到登录页面
    func pushToLogin(type: QSLLoginJumpType) {
        
        let vc = QSLLoginViewController()
        vc.type = type
        self.rootViewController()?.pushVC(vc: vc)
    }
    
    // 跳转到紧急联系人页面
    func pushToContact(type: QSLContactJumpPage) {
        
        let vc = QSLContactController()
        vc.type = type
        self.rootViewController()?.pushVC(vc: vc)
    }
    
    // 跳转到轨迹页面
    func pushToRoad(type: QSLRoadJumpType, model: QSLUserModel) {
        
        let vc = QSLRoadController(userModel: model)
        vc.type = type
        self.rootViewController()?.pushVC(vc: vc)
    }
    
    // 跳转到添加好友页面
    func pushToAdd(type: QSLAddJumpType) {
        let vc = QSLAddController()
        vc.type = type
        self.rootViewController()?.pushVC(vc: vc)
    }
    
    // 跳转到会员试用页面
    func pushToVipTrail(type: QSLTrialVipJumpType) {
        let vc = QSLVipTrialVC()
        vc.type = type
        self.rootViewController()?.pushVC(vc: vc)
    }
    
    // 跳转到意见反馈页面
    func pushToFeedback() {
        let vc = QSLFeedbackVC()
        self.rootViewController()?.pushVC(vc: vc)
    }
    
    // 跳转到客服页面
    func pushToCustomer() {
        if(!QSLBaseManager.shared.isLogin()){
            return
        }
        
        let phone = QSLBaseManager.shared.userModel.phone
        let vc = QSLWebViewController()
        vc.webUrl = "https://qiyu-kefu.atmob.com?ssid=\(phone)&device_id=\(phone)&app_platform=2&app_version=\(QSLApi.appVersionName)&package_name=\(QSLApi.packageName)&app_name=\(QSLApi.appName)&phone=\(phone)"
        vc.isShowTop = true
        self.rootViewController()?.present(vc, animated: true)
    }
}

