package com.themes.fancy.keyboard.keyboard.viewui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.themes.fancy.keyboard.keyboard.MyApplication
import com.themes.fancy.keyboard.keyboard.R
import com.themes.fancy.keyboard.keyboard.data.KbSource
import com.themes.fancy.keyboard.keyboard.helper.Common
import com.themes.fancy.keyboard.keyboard.helper.Common.loadWepJif
import com.themes.fancy.keyboard.keyboard.myada.MainAdapter

class MainActivity : AppCompatActivity() {

    private lateinit var forYouRecycler: RecyclerView
    private lateinit var viewAllLayout: LinearLayout
    private lateinit var cardView:CardView
    private lateinit var banner:ImageView

    private   var dialog: DialogActive?= null

    private    var bannerKbSource: KbSource? = null

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_main_activity)
        Common.initFullScreen(this)
        findViewId()
        initDialog()
        loadBanner()
        setForYouData()
        onClick()
    }

    private fun findViewId() {
        forYouRecycler = findViewById(R.id.for_you_recycler)
        viewAllLayout = findViewById(R.id.layout_view_all)



        cardView = findViewById(R.id.card_banner)

        banner = findViewById(R.id.image_banner)
    }


    private fun initDialog(){
        val checkEnable = Common.checkEnable(MyApplication.appInstance)
        val checkSetDefault = Common.checkSetDefault(MyApplication.appInstance)
        if(!checkEnable||!checkSetDefault){
            dialog = dialog?: DialogActive {

            }
            dialog?.show(supportFragmentManager,"")
        }

    }

    private fun onClick() {
        cardView.setOnClickListener {
            if(bannerKbSource == null){

                return@setOnClickListener
            }
            val intentApply = Intent(this, ApplyActivity::class.java)
            intentApply.putExtra(ApplyActivity.SOURCE_KEY, bannerKbSource)
            intentApply.putExtra(ApplyActivity.DISPLAY_URL_KEY, bannerKbSource!!.imgPath)
            intentApply.putExtra(ApplyActivity.ZIP_URL_KEY, bannerKbSource!!.zipPath)
            intentApply.putExtra(ApplyActivity.NAME_KEY, bannerKbSource!!.titleName)
            intentApply.putExtra(ApplyActivity.GIF_KEY, bannerKbSource!!.imgGif)
            if(!bannerKbSource!!.thumbGif.isNullOrEmpty()){
                intentApply.putExtra(ApplyActivity.THUMB_KEY,bannerKbSource!!.thumbGif)
            }else{
                intentApply.putExtra(ApplyActivity.THUMB_KEY,bannerKbSource!!.thumbUrl)
            }
            startActivity(intentApply)
        }
        viewAllLayout.setOnClickListener {
            startActivity(Intent(this, ViewActivity::class.java))
        }

    }

    private fun setForYouData() {
        val forYouList = MyApplication.list.filter {
            it.parentName == "for you"

        }

        forYouRecycler.run {
            adapter = MainAdapter(
                this@MainActivity
            ).apply {
                val keyboardList = forYouList[0].keyboardList.shuffled()
                setForYouList(keyboardList)
            }
            layoutManager = GridLayoutManager(this@MainActivity, 2)

        }

    }

    private fun loadBanner(){
        val dataClass = MyApplication.list.shuffled()[0]
        bannerKbSource = dataClass.keyboardList.shuffled()[0]

        val thumbGif = bannerKbSource?.thumbGif
        val thumb = bannerKbSource?.thumbUrl
        if (thumbGif != null) {
            if (thumbGif.isNotEmpty()) {
                loadWepJif(this, thumbGif, banner)
            } else if(!thumb.isNullOrEmpty()){
                Glide.with(this).load(thumb).into(banner)
            }
        }
    }


}