package com.blue.running.keyboard.run_frag

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.blue.running.keyboard.R
import com.blue.running.keyboard.RunningApp
import com.blue.running.keyboard.databinding.DialogMoreBinding
import com.blue.running.keyboard.run_activity.ActivitySetThemes
import com.blue.running.keyboard.run_manager.ComUttils.loadWepJif
import com.bumptech.glide.Glide


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


    private lateinit var dialogVb: DialogMoreBinding

    private lateinit var data: com.blue.running.keyboard.run_jsonbean.ChildData


    private var inputManager: InputMethodManager? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        dialogVb = DialogMoreBinding.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(), ActivitySetThemes::class.java)
            intent.putExtra(ActivitySetThemes.SOURCE_KEY, data)
            startActivity(intent)
            dismiss()
        }

    }

    private fun initLoadIm() {
        val list = RunningApp.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(RunningApp.runningAppInstance, thumbGif, dialogVb.imageview)
            } else {
                Glide.with(RunningApp.runningAppInstance)
                    .load(thumb)
                    .placeholder(R.drawable.run_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) =
            DialogMore(onClose)
    }
}