package com.wall.photography.wallpaper.activivty

import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.wall.photography.wallpaper.R
import com.wall.photography.wallpaper.databinding.ActivityAboutBinding
import com.wall.photography.wallpaper.topon.AdManager


class AboutActivity : AppCompatActivity() {
    private lateinit var vb: ActivityAboutBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        vb = ActivityAboutBinding.inflate(layoutInflater)
        setContentView(vb.root)
        AdManager.loadAllAd()
        val appVersionName = getAppVersionName()
        appVersionName?.run {
            vb.tvVersion.text = String.format(getString(R.string.version), this.versionName)
        } ?: run {
            vb.tvVersion.text = String.format(getString(R.string.version), "1.0.0")
        }
        vb.back.setOnClickListener {
            AdManager.showTopOn(this@AboutActivity) {
                finish()
            }
        }
        vb.layoutRate.setOnClickListener {
            try {
                val uri = String.format(getString(R.string.google_link), packageName)
                startActivity(Intent(Intent.ACTION_VIEW).apply {
                    data = Uri.parse(uri)
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                })
            } catch (e: Exception) {
                Toast.makeText(
                    this@AboutActivity,
                    this@AboutActivity.resources.getString(R.string.to_gp_exception),
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        vb.layoutPrivacy.setOnClickListener {
            AdManager.showTopOn(this@AboutActivity) {
                startActivity(Intent(this@AboutActivity, PrivacyActivity::class.java))
            }
        }
    }


    override fun onBackPressed() {
        super.onBackPressed()
        AdManager.showTopOn(this@AboutActivity) {

        }
    }

    private fun getAppVersionName(): PackageInfo? {
        return try {
            packageManager.getPackageInfo(packageName, 0)

        } catch (e: PackageManager.NameNotFoundException) {
            null
        }
    }
}