package com.input.board.appskey.keyboard.viewUi

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.cardview.widget.CardView
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.input.board.appskey.keyboard.KeyBoardApp
import com.input.board.appskey.keyboard.R
import com.input.board.appskey.keyboard.dialog.StepDialog
import com.input.board.appskey.keyboard.fileTools.InputMethodCom
import com.input.board.appskey.keyboard.recyAdapter.ForYouAdapter

class HomeActivity : AppCompatActivity() {

    private lateinit var forYouRecycler: RecyclerView
    private lateinit var viewAllLayout: LinearLayout

    private lateinit var menuView: ImageView
    private lateinit var drawView: DrawerLayout

    private lateinit var keySetting: LinearLayout
    private lateinit var layoutRate: LinearLayout
    private lateinit var layoutPrivacy: LinearLayout

    private lateinit var cardView:CardView

    private   var dialog: StepDialog?= null

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

    private fun findViewId() {
        forYouRecycler = findViewById(R.id.for_you_recycler)
        viewAllLayout = findViewById(R.id.layout_view_all)
        menuView = findViewById(R.id.imageMenu)
        drawView = findViewById(R.id.drawLayout)
        keySetting = findViewById(R.id.layout_keyboard_setting)
        layoutRate = findViewById(R.id.layout_rate)
        layoutPrivacy = findViewById(R.id.layout_privacy)
        cardView = findViewById(R.id.card_banner)
    }


    private fun initDialog(){
        val checkEnable = InputMethodCom.checkEnable(KeyBoardApp.appInstance)
        val checkSetDefault = InputMethodCom.checkSetDefault(KeyBoardApp.appInstance)
        if(!checkEnable||!checkSetDefault){
            dialog = dialog?: StepDialog {

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

    }

    private fun onClick() {
        cardView.setOnClickListener {
            startActivity(Intent(this, ViewAllActivity::class.java))
        }
        viewAllLayout.setOnClickListener {
            startActivity(Intent(this, ViewAllActivity::class.java))
        }
        menuView.setOnClickListener {
            drawView.openDrawer(GravityCompat.END)
        }
        keySetting.setOnClickListener {
            startActivity(Intent(this, KeyBoardSettingActivity::class.java))
        }
        layoutRate.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse(getString(R.string.app_google_play)+packageName)
            startActivity(intent)
        }
        layoutPrivacy.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse(getString(R.string.app_privacy_path))
            startActivity(intent)
        }

    }

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

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

        }

    }


}