package com.ydl.view

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.DialogFragment
import com.blankj.utilcode.util.KeyboardUtils
import com.ydl.webview.R
import com.ydl.ydlcommon.utils.remind.ToastHelper

class ArticleDialogFragment: DialogFragment() {

    private lateinit var ivClose: ImageView
    private lateinit var etComment: EditText
    private lateinit var tvPublish: TextView
    private lateinit var cslLayout:ConstraintLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.BottomDialogAnimation)
    }

    override fun onStart() {
        super.onStart()
        val displayMetrics = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
        val window = dialog?.window
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.layout_comment_article,container)
        initView(view)
        return view
    }

    private fun initView(view: View){
        ivClose = view.findViewById(R.id.iv_close_dialog)
        etComment = view.findViewById(R.id.et_article_comment)
        tvPublish = view.findViewById(R.id.tv_publish_comment)
        cslLayout = view.findViewById(R.id.csl_root_layout)

        cslLayout.setOnClickListener {
            dismiss()
        }

        KeyboardUtils.showSoftInput(etComment)

        ivClose.setOnClickListener {
            dismiss()
        }
        tvPublish.setOnClickListener {
            if (etComment.text.toString().isNullOrBlank()){
                ToastHelper.show("评论内容不能为空～")
                return@setOnClickListener
            }
            dismiss()
        }

    }

    override fun dismiss() {
        KeyboardUtils.hideSoftInput(etComment)
        super.dismiss()
    }

}