package com.themes.keyboard.black.fragment

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.themes.keyboard.black.MyApp
import com.themes.keyboard.black.R
import com.themes.keyboard.black.activity.ApplyActivity
import com.themes.keyboard.black.databinding.DialogRecommendBinding
import com.themes.keyboard.black.localdata.ChildData
import com.themes.keyboard.black.utils.Common.loadWepJif


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


    private lateinit var dialogVb: DialogRecommendBinding

    private lateinit var data: ChildData




    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        dialogVb = DialogRecommendBinding.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 = 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) =
            DialogRecommend(onClose)
    }
}