//
//  DialogueViewController+http.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/10.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension DialogueViewController {

    func getDialogueHttpPostCmd(){
        let cmd:HttpCommand = DialogueHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpDialogueHttpPostResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpDialogueHttpPostCmd_pageNum] = 1
        dic[kHttpDialogueHttpPostCmd_pageSize] = 100
        let filtersArray = NSMutableArray()
        let pidDic:[String:Any] = ["key":"pid","opt":"EQ","type":"S","value":"1"]
        let foldertypeDic:[String:Any] = ["key":"foldertype","opt":"EQ","type":"S","value":judgmentStrNil(str: AccountInfo.sharedInstance().infoCompanyName)]
        filtersArray.add(pidDic)
        filtersArray.add(foldertypeDic)
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpDialogueHttpPostCmd_empid] = AccountInfo.sharedInstance().UserId!
//        attrDic[kHttpDialogueHttpPostCmd_filterString] = self.inputBoxView.dialogueTextF.text
        attrDic[kHttpDialogueHttpPostCmd_filterString] = self.inputBoxView.dialogueTextF.text
        dic[kHttpCustomerServiewHttpCmd_filters] = filtersArray
        dic[kHttpCustomerServiewHttpCmd_attr] = attrDic
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpDialogueHttpPostResultResponse(result:RequestResult){
        let r:DialogueHttpPostResult = result as! DialogueHttpPostResult
        if r.isOk() {
            let httpArray = r.getDialogueHttpPostResult()
            self.grouping(listModel: httpArray[0] as! DialogueListModel)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    /// 将文字发送给后台，后台数据分组
    func grouping(listModel:DialogueListModel){
        if listModel.titleArray.count == 0 {
            self.addLeft(title: "没有相关内容，请重新输入~~")
            return
        }
        let leftCellModel = DialogueLeftSelectionTVCModel()
        leftCellModel.className = "DialogueLeftSelectionTVC"
        leftCellModel.leftCellH = 0
        for index in 0..<listModel.titleArray.count {
            let typename:String = listModel.titleArray[index] as! String
            let selectionModel = SelectionModel()
            selectionModel.typename = typename
            selectionModel.intervalH = 5
            selectionModel.titleH = getTextHeigh(textStr: typename, font: UIFont.test_systemFont(ofSize: 13.33), width: SCREEN_WHIDTH()-160.67) + selectionModel.intervalH
//            print(typename,selectionModel.titleH)
            leftCellModel.leftCellH = leftCellModel.leftCellH + selectionModel.titleH
            for i in 0..<listModel.itemModelArray.count {
                let itemModel:DialogueListItemModel = listModel.itemModelArray[i] as! DialogueListItemModel
                itemModel.itemH = getTextHeigh(textStr: itemModel.title!, font: UIFont.test_systemFont(ofSize: 13.33), width: SCREEN_WHIDTH()-160.67) + selectionModel.intervalH
                if (selectionModel.typename == itemModel.typename) {
                    leftCellModel.leftCellH = leftCellModel.leftCellH + itemModel.itemH
                    selectionModel.selectItemArray.add(itemModel)
                }
                itemModel.block = {[weak self] in
                    self?.selectItemModel = itemModel
                    self?.actionContentHttpGCD()
                }
//                print(itemModel.title!,itemModel.itemH)
            }
            leftCellModel.selectionArray.add(selectionModel)
        }
        self.cellItemListH = leftCellModel.leftCellH+60+self.cellItemListH
//        print(leftCellModel.leftCellH)
        self.dataSource.add(leftCellModel)
    }
    /// 点击后台数据
    func getDialogueCountentHttpGetCmd(){
        let cmd:HttpCommand = DialogueCountentHttpGetCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpDialogueCountentHttpGetResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpDialogueCountentHttpGetCmd_id] = judgmentStrNil(str: self.selectItemModel.id)
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpDialogueCountentHttpGetResultResponse(result:RequestResult){
        let r:DialogueCountentHttpGetResult = result as! DialogueCountentHttpGetResult
        if r.isOk() {
            let httpArray = r.getDialogueCountentHttpGetResult()
            self.getContentCellModel(contentModel: httpArray[0] as! DialogueCountentModel)
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    /// 处理content
    func getContentCellModel(contentModel:DialogueCountentModel){
        let cellModel = DialogueLeftContentTVCModel()
        cellModel.className = "DialogueLeftContentTVC"
        cellModel.titleStr = contentModel.title
        cellModel.content = contentModel.content
        cellModel.titleH = getTextHeigh(textStr: contentModel.title!, font: UIFont.test_systemFont(ofSize: 13.33), width: SCREEN_WHIDTH()-160.67)
        if (contentModel.content == nil || contentModel.content?.count == 0) {
            cellModel.leftCellH = 0
        } else {
            cellModel.leftCellH = self.getCountentH(str: contentModel.content!)
        }
        self.cellItemListH = cellModel.leftCellH+60+self.cellItemListH+cellModel.titleH
        self.dataSource.add(cellModel)
    }
    func getCountentH(str:String)->CGFloat {
        let attrStr = try! NSMutableAttributedString(
            data: (str.data(using: .unicode, allowLossyConversion: true)!),
            options:[.documentType: NSAttributedString.DocumentType.html,
                     .characterEncoding: String.Encoding.utf8.rawValue],
            documentAttributes: nil)
        //行高
//        let paraph = NSMutableParagraphStyle()
//        paraph.lineSpacing = 1
//        attrStr.addAttributes([NSAttributedStringKey.paragraphStyle:paraph],
//                              range: NSMakeRange(0, attrStr.length))
        let contentHegiht = attrStr.boundingRect(with: CGSize(width:  SCREEN_WHIDTH()-160.67, height: 1000), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).height
//        print(contentHegiht)
        return contentHegiht
    }
}
