package com.various.funny.sounds.audio.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.applovin.mediation.MaxAd
import com.applovin.mediation.ads.MaxInterstitialAd
import com.various.funny.sounds.audio.databinding.ActivitySourceBinding
import com.various.funny.sounds.audio.helper.ComUtils
import com.various.funny.sounds.audio.helper.ItemSpace
import com.various.funny.sounds.audio.max.MaxListener
import com.various.funny.sounds.audio.max.MaxManager
import com.various.funny.sounds.audio.ui.adapter.SourceAdapter

class SourceActivity : AppCompatActivity() {
    private lateinit var sourceBinding: ActivitySourceBinding

    private lateinit var mAdapter: SourceAdapter
    private lateinit var viewModel: SourceViewModel

    companion object {
        val INTENT_KEY_ID = "key_class_id"
    }

    private lateinit var id: String
    private lateinit var listAds: List<MaxInterstitialAd>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        listAds = MaxManager.loadAd(this@SourceActivity)

        sourceBinding = ActivitySourceBinding.inflate(layoutInflater)
        setContentView(sourceBinding.root)
        id = intent.getStringExtra(INTENT_KEY_ID)!!
        viewModel = ViewModelProvider(this)[SourceViewModel::class.java]
        viewModel.query(id)
        mAdapter = SourceAdapter(this@SourceActivity)
        viewModel.getList().observe(this) {
            mAdapter.updateData(it)
        }
        initStatusBar()
        onInitSource()
        sourceBinding.iconBack.setOnClickListener {
            showAd(true)
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        showAd(false)
    }

    private fun showAd(needFinish: Boolean) {
        val checkCacheAd = MaxManager.checkCacheAd(listAds)
        if (checkCacheAd != null) {
            MaxManager.setMaxListener(checkCacheAd, object : MaxListener {
                override fun onFail(ad: MaxAd?) {
                    if (needFinish) {
                        finish()
                    }

                }

                override fun onSuccess() {

                }

                override fun onHidden() {
                    if (needFinish) {
                        finish()
                    }

                }

            })
            checkCacheAd.showAd()
        } else {
            if (needFinish) {
                finish()
            }
        }
    }

    private fun onInitSource() {
        val i = ComUtils.getScreenWidthPx() - ComUtils.dpToPx(110f) * 3
        val spacePx = i / 4

        sourceBinding.recyclerViewSource.run {
            adapter = mAdapter
            layoutManager = GridLayoutManager(this@SourceActivity, 3)
            addItemDecoration(ItemSpace(spacePx))
            setPadding(spacePx, ComUtils.dpToPx(6f), 0, 0)
        }
    }

    private fun initStatusBar() {
        val flags = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.decorView.systemUiVisibility = flags xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }
}