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

import UIKit

class VoiceDialogueVC: BaseViewController {

    lazy var myTableView: UITableView = {
        let myTableView = UITableView.init()
        self.view.addSubview(myTableView)
        myTableView.snp.makeConstraints { (make) in
            make.width.height.equalToSuperview()
            make.center.equalToSuperview()
        }
        if #available(iOS 11.0, *) {
            myTableView.contentInsetAdjustmentBehavior = .never
            myTableView.scrollIndicatorInsets = myTableView.contentInset
            myTableView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height+44, 0, 46.67+HEIGHT_BOTTOM_MARGIN(), 0)
        }else{
            myTableView.contentInset = UIEdgeInsetsMake(0, 0, 46.67+HEIGHT_BOTTOM_MARGIN(), 0)
        }
        registerCell(myTableView, cell: VoiceDialogueRightTVC.self)
        registerCell(myTableView, cell: VoiceDialogueLeftTVC.self)
        registerCell(myTableView, cell: VoiceDialogueLeftAppTVC.self)
        registerCell(myTableView, cell: DialogueLeftContentTVC.self)
        myTableView.backgroundColor = UIColorFromRGB(rgbValue: 0xE9F2F8)
        setExtraCellLineHidden(tableView: myTableView)
        return myTableView
    }()
    lazy var inputBoxView: VoiceDialogueInputView = {
        let inputBoxView = VoiceDialogueInputView.init(frame: CGRect(x:0, y: SCREEN_HEIGHT()-(46.67+HEIGHT_BOTTOM_MARGIN()), width:SCREEN_WHIDTH() , height:46.67+HEIGHT_BOTTOM_MARGIN()))
        return inputBoxView
    }()
    
    var delegate:VoiceDialogueVCDelegate!
    var dataSource = NSMutableArray()
    var mUserInfo: Dictionary<AnyHashable, Any>!
    var mKeyBoardHeight: CGFloat!
    var mKeyBoardAnimateDuration: Double!
    var selectItemModel = DialogueListItemModel()
    var cellItemListH:CGFloat!
    var cellItemH:CGFloat!
    var tableViewTransformH:CGFloat!
    var voiceTest:String?
    var secectModel = XiaocaiQuerysModel()
    
    var companyListModelArray = NSMutableArray()
    var departmentListModelArray = NSMutableArray()
    var selecClassModel:ApplicationHttpListModel!
    
    /// 判断进入方式
    var gotoVoiceDialogueType:GotoVoiceDialogueType = .NomVoiceDialogue
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        SuspensionView.share().getsuspensionBtn()?.isHidden = true
        initTitleBar()
        self.title = "小彩同学"
        self.view.backgroundColor = UIColorFromRGB(rgbValue: 0xE9F2F8)
        mKeyBoardHeight = 0
        cellItemListH = 0
        cellItemH = 90
        tableViewTransformH = 0
        addInitTableView()
        self.view.addSubview(inputBoxView)
        voviceMessage()
        BDVoiceWakeUp.share().stopWakeup()
        self.appViewsSstatistics(title: "小彩同学", scope: AppClickType.MyAppType.rawValue)
        if self.gotoVoiceDialogueType == .VoiceDialogueWeakUp {
            inputBoxView.beginRecordVoice()
        }
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        SuspensionView.share().getsuspensionBtn()?.isHidden = false
    }
    override func initReturnBtn(){
        let leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "return_image")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(arrowResponse))
        self.navigationItem.leftBarButtonItem = leftN
//        let rightN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "return_image")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(touchesBega))
//        self.navigationItem.rightBarButtonItem = rightN
    }
    @objc override func arrowResponse(){
        BDVoiceWakeUp.share().startWakeup()
        inputBoxView.cancelRecordVoice()
        self.navigationController?.popViewController(animated: true)
    }
    /// 语音文本发送给后台
    @objc func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getXiaocaiQuerysHttpPostCmd()
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.delegate.dataSource = self?.dataSource
            self?.myTableView.delegate = self?.delegate
            self?.myTableView.reloadData()
            self?.updateTableViewUI()
            self?.hidenHintView()
        }
    }
    func updateTableViewUI() {
        if self.dataSource.count <= 0 {
            return
        }
        setTableViewTransform()
        //tableView 更新界面操作
        let indexPath = NSIndexPath.init(row: self.dataSource.count-1, section: 0)
        DispatchQueue.global().async {
            //print("async do something\(Thread.current)")
            DispatchQueue.main.sync {
                self.myTableView.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
                self.myTableView.transform = CGAffineTransform(translationX: 0, y: -self.tableViewTransformH)
            }
        }
    }
    ///依据内容实现myTableView的移动
    func setTableViewTransform() {
        //        print(self.cellItemListH)
        let h = SCREEN_HEIGHT()-(46.67+HEIGHT_BOTTOM_MARGIN())-self.mKeyBoardHeight-NAV_HEIGHT()-STATUBAR_HEIGHT()
        if self.cellItemListH <= h {
            tableViewTransformH = 0
        } else if self.cellItemListH > h && self.cellItemListH < SCREEN_HEIGHT()-(46.67+HEIGHT_BOTTOM_MARGIN()) {
            tableViewTransformH = self.cellItemListH-h
        } else if self.cellItemListH >= SCREEN_HEIGHT()-(46.67+HEIGHT_BOTTOM_MARGIN()){
            tableViewTransformH = self.mKeyBoardHeight
        }
    }
    /// 应用访问量
    func appViewsSstatistics(title:String,scope:String){
        let model = ClickRateModel()
        model.name = title
        model.belong = "门户"
        model.scope = scope
        model.type = "1"
        self.getClickRateHttpCmd(infoModel: model)
    }
}
