package com.xm.test.myfilemaster.view

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.xm.test.myfilemaster.R

class CustomDialog(context: Context?) : Dialog(context!!), View.OnClickListener{
    private lateinit var mEdit: EditText
    private lateinit var mCancel: TextView
    private lateinit var mSure: TextView
    private var mTitle: TextView? = null
    private var mHint: TextView? = null
    private var mCancelListener: View.OnClickListener? = null
    private var mSureListener: View.OnClickListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_custom_layout)
        setCancelable(false)

        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val windowManager = window?.windowManager
        val display = windowManager?.defaultDisplay
        val params = window?.attributes
        val size = Point()
        display?.getSize(size)
        params?.width = ((size.x) * 0.959).toInt()
        window?.attributes = params

        mEdit = findViewById(R.id.dialog_edit)
        mCancel = findViewById(R.id.dialog_cancel_btn)
        mSure = findViewById(R.id.dialog_sure_btn)

        mTitle = findViewById(R.id.dialog_title_text)
        mHint = findViewById(R.id.dialog_hint_text)

        mCancel.setOnClickListener(this)
        mSure.setOnClickListener(this)


    }

    fun setsConfirm(sure: View.OnClickListener): CustomDialog{
        this.mSureListener = sure
        return this
    }
    fun setsCancel(cancel: View.OnClickListener): CustomDialog{
        this.mCancelListener = cancel
        return this
    }
    fun setsTitle(str: String): CustomDialog{
        findViewById<TextView>(R.id.dialog_title_text).text = str
        return this
    }
    fun setsHint(str: String): CustomDialog{
        findViewById<TextView>(R.id.dialog_hint_text).text = str
        return this
    }
    fun setsRightDelete(str: String): CustomDialog{
        mSure.text = str
        mSure.setTextColor(Color.RED)
        mSure.setBackgroundResource(R.drawable.bg_btn_cancel_shape)
        return this
    }
    fun isShowEditText(isShow : Boolean): CustomDialog{
        mEdit.visibility = if (isShow) View.VISIBLE else View.GONE
        return this
    }
    fun isShowHintText(isShow : Boolean): CustomDialog{
        findViewById<TextView>(R.id.dialog_hint_text).visibility = if (isShow) View.VISIBLE else View.GONE
        return this
    }

    fun getEditTextStr(): String{
        return mEdit.text.toString()
    }

    override fun onClick(v: View?) {
        when (v?.id){
            R.id.dialog_cancel_btn ->{
                if (mCancelListener != null){
                    mCancelListener?.onClick(v)
                }
            }
            R.id.dialog_sure_btn ->{
                if (mSureListener != null){
                    mSureListener?.onClick(v)
                }
            }
        }
    }
}