//
//  VoiceDialogueVC+http.swift
//  MobilePortal
//
//  Created by caiyang on 2020/5/18.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension VoiceDialogueVC {
    /// 语音转换
    func getXiaocaiQuerysHttpPostCmd(){
        let cmd:HttpCommand = XiaocaiQuerysHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpXiaocaiQuerysHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpXiaocaiQuerysHttpPostCmd_input] = judgmentStrNil(str: voiceTest)
        dic[kHttpXiaocaiQuerysHttpPostCmd_userid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpXiaocaiQuerysHttpResultResponse(result:RequestResult){
        let r:XiaocaiQuerysHttpResult = result as! XiaocaiQuerysHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getXiaocaiQuerysHttpResult() as NSMutableArray
            self.addVoiceDialogueLeftAppTVCModel(array: array)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
            addLeft(title: "没有相关内容，请重新输入~~")
        }
        group.leave()
    }
    /// 语音转文字 数据处理
    func addVoiceDialogueLeftAppTVCModel(array:NSMutableArray) {
        if array.count == 0 {
            addLeft(title: "没有相关内容，请重新输入~~")
            return
        }
        let cellModel = VoiceDialogueLeftAppTVCModel()
        cellModel.className = "VoiceDialogueLeftAppTVC"
        cellModel.titleStr = "你想寻找的是？"
        for index in 0..<array.count {
            cellModel.appArray.add(self.getAppInfoModel(model: array[index] as! ApplicationHttpListModel))
        }
        if array.count == 1 {
            self.classAction(selecHttpModel: array[0] as! ApplicationHttpListModel)
        }
        self.dataSource.add(cellModel)
    }
    /// 获取appx信息
    func getAppInfoModel(model:ApplicationHttpListModel)->VoiceDialogueLeftAppCTCModel{
        let collectionCellModel = VoiceDialogueLeftAppCTCModel()
        collectionCellModel.className = "VoiceDialogueLeftAppCTC"
        collectionCellModel.titleValue = model.name
        collectionCellModel.cellW = getTexWidth(textStr: model.name!, font: UIFont.test_systemFont(ofSize: 12), height: 60) + 40
        collectionCellModel.block = {[weak self](value:Any) in
//            self?.classAction(selecHttpModel: model)
            self?.refreshTokenHttpAction(selecHttpModel: model)
        }
        return collectionCellModel
    }
    /// 获取App列表
    func getXiaocaiQueryAppXinfoHttpPostCmd(){
        let cmd:HttpCommand = XiaocaiQueryAppXinfoHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpXiaocaiQueryAppXinfoHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let filtersArr = NSMutableArray()
        let filtersDic = NSMutableDictionary()
        filtersDic[kHttpXiaocaiQueryAppXinfoHttpPostCmd_key] = "name"
        filtersDic[kHttpXiaocaiQueryAppXinfoHttpPostCmd_opt] = "LIKE"
        filtersDic[kHttpXiaocaiQueryAppXinfoHttpPostCmd_type] = "S"
        filtersDic[kHttpXiaocaiQueryAppXinfoHttpPostCmd_value] = judgmentStrNil(str: self.secectModel.output)
        filtersArr.add(filtersDic)
        dic[kHttpXiaocaiQueryAppXinfoHttpPostCmd_filters] = filtersArr
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpXiaocaiQueryAppXinfoHttpResultResponse(result:RequestResult){
        let r:XiaocaiQueryAppXinfoHttpResult = result as! XiaocaiQueryAppXinfoHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getXiaocaiQueryAppXinfoHttpResult() as NSMutableArray
//            self.addVoiceDialogueLeftAppTVCModel(array: array)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
//        group.leave()
    }
    
}
