package com.color.keyboard.black.myfragment

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.DialogFragment
import com.bumptech.glide.Glide
import com.color.keyboard.black.ColorApp
import com.color.keyboard.black.R
import com.color.keyboard.black.activity.ApplyActivity
import com.color.keyboard.black.bean.ChildData
import com.color.keyboard.black.databinding.DialogRecomBinding
import com.color.keyboard.black.manager.Common.loadWepJif


class DialogRecom(var onClose: () -> Unit) : DialogFragment() {


    private lateinit var dialogVb: DialogRecomBinding

    private lateinit var data: ChildData


    private var inputManager: InputMethodManager? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        dialogVb = DialogRecomBinding.inflate(inflater, container, false)
        init()
        return dialogVb.root
    }


    private fun init() {
        isCancelable = false
        val window = dialog!!.window
        window!!.setBackgroundDrawableResource(R.color.transparent)
        window.decorView.setPadding(0, 0, 0, 0)
        initLoadIm()
        initBar(window)

        val wlp = window.attributes
        wlp.gravity = Gravity.CENTER
        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = wlp

        dialogVb.close.setOnClickListener {
            dismiss()

        }

        dialogVb.tvApply.setOnClickListener {
            val intent = Intent(requireContext(), ApplyActivity::class.java)
            intent.putExtra(ApplyActivity.SOURCE_KEY, data)
            startActivity(intent)
            dismiss()
        }

    }

    private fun initLoadIm() {
        val list = ColorApp.list.filter {
            it.parentName == getString(R.string.recommend_name)

        }
        val shuffled = list[0].keyboardList.shuffled()
        shuffled[0].let {
            data = it
            val thumbGif: String = it.thumbGif
            val thumb: String = it.thumbUrl
            if (thumbGif.isNotEmpty()) {
                loadWepJif(ColorApp.colorAppInstance, thumbGif, dialogVb.imageview)
            } else {
                Glide.with(ColorApp.colorAppInstance)
                    .load(thumb)
                    .placeholder(R.drawable.im_placeholder)
                    .thumbnail(0.5f)
                    .into(dialogVb.imageview)
            }
        }

    }


    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onClose.invoke()
    }


    private fun initBar(window: Window) {
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
    }

    companion object {
        @JvmStatic
        fun newInstance(onClose: () -> Unit) =
            DialogRecom(onClose)
    }
}