//
//  MineMoreViewController+ClassAction.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/21.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension MineMoreViewController {
    /// MARK: 刷新RefreshToken
    func refreshTokenHttpAction(selecHttpModel:ApplicationHttpListModel) {
        self.selecClassModel = selecHttpModel
        if (AccountInfo.sharedInstance().loginType != nil && AccountInfo.sharedInstance().loginType == "1") {
            SinglePointWeChatHttp.sharedInstance().judgmentTokenExpired(resultBlock: {[weak self] (value) in
                self?.classAction(selecHttpModel: selecHttpModel)
            }) { [weak self] (value) in
                self?.classAction(selecHttpModel: selecHttpModel)
            }
        } else {
            self.classAction(selecHttpModel: selecHttpModel)
        }
    }
    //点击应用响应事件
    func classAction(selecHttpModel:ApplicationHttpListModel){
        self.selecClassModel = selecHttpModel
        if AccountInfo.sharedInstance().oldType == "0" {
            self.classActionOld(selecHttpModel: selecHttpModel, type: self.applicationType)
            return
        }
        if selecHttpModel.pointstate == "0" {
            SMAlertView.showAlert("单点未开启,请联系管理员！")
            return
        }
        if selecHttpModel.applicationPlatformsType == .AndroidiOSType {
            myApplicationAction(httpDetailModel: selecHttpModel)
        } else {
            oftenApplicationType(httpDetailModel: selecHttpModel)
        }
    }
    //我的应用响应事件
    private func myApplicationAction(httpDetailModel:ApplicationHttpListModel){
        if self.isInstallation(urlString: httpDetailModel.iosopenurl!) == false{
            //打开APP
            
            self.downloadOpenApp(urlString: self.getSignInAction(model: httpDetailModel), openUrlType: .OpenAppType)
        }else{
            if httpDetailModel.iosurl?.count == 0{
                SMToastView.showMessage("该应用暂无下载地址，请联系管理员！")
                return
            }
            self.downloadOpenApp(urlString: httpDetailModel.iosurl!, openUrlType: .DownloadType)
        }
    }
    //我的常用响应事件
    func oftenApplicationType(httpDetailModel:ApplicationHttpListModel){
        let companyInfo:SelecCompanyDepartmentInfo = getSelecCompanyDepartment(tags: httpDetailModel.sysid ?? String())
        if companyInfo.tags == nil {
            getSelectCompanyHttpCmd()
        }else{
            if httpDetailModel.sysid == "ERP" && companyInfo.sysOrg == ""{
                SMToastView.showMessage("ERP组织获取失败！")
                hidenHintView()
                return
            }
            self.setCacheAccountInfo(companyInfo: companyInfo, selecClassModel: self.selecClassModel)
            return
        }
    }
    /// 点击响应事件
    func classActionOld(selecHttpModel:ApplicationHttpListModel,type:ApplicationType){
        self.selecClassModel = selecHttpModel
        if type == .MyApplicationType {
            self.myApplicationActionOld(httpDetailModel: selecHttpModel)
        } else {
            self.oftenApplicationTypeOld(httpDetailModel: selecHttpModel)
        }
    }
    /// 我的应用
    func myApplicationActionOld(httpDetailModel:ApplicationHttpListModel){
        if httpDetailModel.comments != nil && httpDetailModel.comments?.count != 0{
            let url:String = httpDetailModel.comments!+"?docId="+judgmentStrNil(str: httpDetailModel.id)+"&userId="+AccountInfo.sharedInstance().UserId!+"&userName="+judgmentStrNil(str: AccountInfo.sharedInstance().userName)
            self.actionGotoTitleWeb(webViewUrl: url, title: "", isHomePushType: .YesIsHomePushType, appid: httpDetailModel.appid!, isEncryptionType: .YesEncryptionType)
            return
        }
        if self.isInstallation(urlString: httpDetailModel.openurl!) == false{
            self.downloadOpenApp(urlString: httpDetailModel.openurl!, openUrlType: .OpenAppType)
        }else{
            if httpDetailModel.appid?.count == 0{
                SMToastView.showMessage("该应用暂无下载地址，请联系管理员！")
                return
            }
            self.downloadOpenApp(urlString: httpDetailModel.appid!, openUrlType: .DownloadType)
        }
    }
    /// 我的常用
    func oftenApplicationTypeOld(httpDetailModel:ApplicationHttpListModel) {
        let companyInfo:SelecCompanyDepartmentInfo = getSelecCompanyDepartment(tags: httpDetailModel.sysid ?? String())
        if companyInfo.tags == nil {
            getSelectCompanyHttpCmd()
        }else{
            if httpDetailModel.sysid == "ERP" && companyInfo.sysOrg == ""{
                SMToastView.showMessage("ERP组织获取失败！")
                hidenHintView()
                return
            }
            self.setCacheAccountInfo(companyInfo: companyInfo, selecClassModel: self.selecClassModel)
            return
        }
    }
}
