package com.sunny.app.soft.timberkeyboardnew.tools

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.provider.Settings
import android.util.Log
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.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import com.anythink.debug.util.a
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.sunny.app.soft.timberkeyboardnew.App
import com.sunny.app.soft.timberkeyboardnew.R
import com.sunny.app.soft.timberkeyboardnew.data.entity.BackgroundEntity
import com.sunny.app.soft.timberkeyboardnew.databinding.DialogRecommendBinding

import com.sunny.app.soft.timberkeyboardnew.databinding.DialogStepBinding
import com.sunny.app.soft.timberkeyboardnew.room.MyDatabase
import com.sunny.app.soft.timberkeyboardnew.ui.activity.DownloadActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


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


    private lateinit var dialogVb: DialogRecommendBinding


    private var inputManager: InputMethodManager? = null

    private var data: BackgroundEntity? = null
    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)

        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.imageview.setOnClickListener {

        }

        CoroutineScope(Dispatchers.IO).launch {
            data = MyDatabase.myDatabase.BackgroundEntityDao().queryRandomItem()
            withContext(Dispatchers.Main) {
                data?.let { backEntity ->
                    Glide
                        .with(App.appContext)
                        .load(backEntity.coverPath)
                        .thumbnail(0.5f)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(dialogVb.imageview)

                    dialogVb.tvApply.setOnClickListener {
                        val intent = Intent(activity, DownloadActivity::class.java)
                        intent.putExtra(AppConstant.KEY_EXTRA, backEntity)
                        startActivity(intent)
                        dismiss()
                    }
                }

            }
        }

    }


    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(activity: Activity, onClose: () -> Unit) =
            DialogRecommend(activity, onClose)
    }
}