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

import UIKit

class VoiceDialogueInputView: UIView,IFLYSpeechTwoDelegate {

    lazy var voiceBtn: UIButton = {
        let voiceBtn = UIButton.init(type: .custom)
        voiceBtn.frame = CGRect(x:15, y: 7, width:SCREEN_WHIDTH()-30 , height:35 )
        voiceBtn.setTitleColor(UIColorFromRGB(rgbValue: 0x4C4C4C), for: .normal)
        voiceBtn.setTitleColor(UIColorFromRGB(rgbValue: 0x4C4C4C), for: .highlighted)
        voiceBtn.setTitle("按住说话", for: .normal)
        voiceBtn.setTitle("松开发送", for: .highlighted)
        voiceBtn.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.flexibleWidth.rawValue | UIViewAutoresizing.flexibleHeight.rawValue)
        voiceBtn.layer.cornerRadius = 4
        voiceBtn.layer.masksToBounds = true
        voiceBtn.addTarget(self, action: #selector(beginRecordVoice), for: .touchDown)
        voiceBtn.addTarget(self, action: #selector(endRecordVoice), for: .touchUpInside)
//        voiceBtn.addTarget(self, action: #selector(cancelRecordVoice), for: UIControlEvents(rawValue: UIControlEvents.touchUpInside.rawValue|UIControlEvents.touchCancel.rawValue))
//        voiceBtn.addTarget(self, action: #selector(RemindDragExit), for: .touchDragExit)
//        voiceBtn.addTarget(self, action: #selector(RemindDragEnter), for: .touchDragEnter)
//        voiceBtn.addTarget(self, action: #selector(beginRecordVoice), for: .touchDown)
        voiceBtn.layer.borderColor = UIColor.lightGray.withAlphaComponent(0.4).cgColor
        voiceBtn.layer.borderWidth = 1
        return voiceBtn
    }()
    var block:passParameterBlock!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColorFromRGB(rgbValue: 0xF3F3F7)
        self.layer.cornerRadius = 1
        self.layer.borderColor = UIColor.lightGray.withAlphaComponent(0.4).cgColor
        self.autoresizingMask = .flexibleWidth
        self.addSubview(voiceBtn)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setInputBoxView() {
        
    }
    /// 开始录音
    @objc func beginRecordVoice() {
        IFLYSpeechTwo.share().startParsing()
        IFLYSpeechTwo.share().delegate = self
    }
    /// 语音结果回掉
    func chatMessage(withSendText text: String) {
        if (self.block != nil) {
            self.block(text)
        }
    }
    /// 停止录音
    @objc func endRecordVoice() {
        IFLYSpeechTwo.share().stopListening()
        SHMessageVoiceHUD.shareInstance()?.hudType = 0
    }
    /// 取消录音
    @objc func cancelRecordVoice() {
        IFLYSpeechTwo.share().cancel()
        SHMessageVoiceHUD.shareInstance()?.hudType = 0
    }
    /// 离开录音
    @objc func RemindDragExit() {
        SHMessageVoiceHUD.shareInstance()?.hudType = 2
    }
    /// 按住提示
    @objc func RemindDragEnter() {
        SHMessageVoiceHUD.shareInstance()?.hudType = 1
    }
}
