package com.yidianling.ydlcommon.custom.dialog

import android.app.Activity
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.ydlcommon.R
import com.yidianling.ydlcommon.custom.emoji.EmojiFilterUtil
import com.yidianling.ydlcommon.custom.utils.InputMethonUtils


/**
 * 通用文本输入框 dialog  如评论
 * Created by harvie on 2017/6/30 0030.
 */
class InputTextDialog : Dialog{

    private var text_cancel : TextView? =null
    private var text_sure : TextView? =null
    private var edit_content : EditText? = null

    private var call : CallBack? =null

    private var hintText = ""

    private var activity : Activity? =null

    constructor(context: Activity?) : super(context, R.style.commonDialog){
        this.activity = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.dialog_input_text)

        initView()

        window.attributes.width = RxDeviceTool.getScreenWidth(context)
        window.setGravity(Gravity.BOTTOM)

        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
    }

    fun initView(){
        text_cancel = findViewById<TextView>(R.id.text_cancel)
        text_sure = findViewById<TextView>(R.id.text_sure)
        edit_content = findViewById<EditText>(R.id.edit_content)
        edit_content?.hint = hintText

        EmojiFilterUtil.filterEmoji(context,edit_content)

        text_cancel?.setOnClickListener {
            call?.cancel(this)
        }

        text_sure?.setOnClickListener {
            call?.commit(this,edit_content?.text.toString())
        }
    }

    //设置回调监听
    fun setCallBackListener(call : CallBack) : InputTextDialog {
        this.call = call
        return this
    }

    //设置hint文字
    fun setHintText(hint : String): InputTextDialog {
        this.hintText = hint
        edit_content?.hint = hint
        return this
    }

    override fun setOnShowListener(listener: DialogInterface.OnShowListener?) {
        super.setOnShowListener(listener)
        //默认弹出输入法
        InputMethonUtils.showSoftInputFromWindow(context,edit_content)
    }


    interface CallBack{

        fun cancel(dia : Dialog)

        fun commit(dia : Dialog,content : String)

    }

    override fun show() {
        if (null == activity){
            return
        }
        if (activity!!.isFinishing){
            return
        }
        super.show()
    }
}