package com.rich.keyboard.skin.uifragment

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 androidx.fragment.app.DialogFragment
import com.bumptech.glide.Glide
import com.rich.keyboard.skin.MyApp
import com.rich.keyboard.skin.R
import com.rich.keyboard.skin.databinding.DiaRecommendBinding
import com.rich.keyboard.skin.localdata.Child
import com.rich.keyboard.skin.uiactivity.ApplyUi
import com.rich.keyboard.skin.utils.MyCommon.loadWepJif


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


    private lateinit var dialogVb: DiaRecommendBinding

    private lateinit var data: Child




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

    }

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