package com.rgt.superr.ui.dialog

import android.app.Activity
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import com.rgt.superr.R
import com.rgt.superr.business.ads.AdsMgr
import com.rgt.superr.databinding.DialogFunctionBackBinding
import com.rgt.superr.ui.appuninstall.AppUninstallActivity
import com.rgt.superr.ui.batteryinfo.BatteryInfoActivity
import com.rgt.superr.ui.clean.JunkCleanActivity
import com.rgt.superr.ui.largefile.LargeFileCleanActivity
import com.rgt.superr.ui.photocompression.PhotoCompressionActivity
import com.rgt.superr.ui.screenshot.ScreenshotCleanActivity
import com.rgt.superr.ui.similar.SimilarPhotosActivity

class FunctionBackDialog(
    val activity: Activity
) {

    val dialog = AlertDialog.Builder(activity).create()
    val binding = DialogFunctionBackBinding.inflate(LayoutInflater.from(activity))


    fun showDialog(back: () -> Unit) {
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        val params = dialog.window?.attributes
//        params?.width = LinearLayout.LayoutParams.MATCH_PARENT
//        params?.height = LinearLayout.LayoutParams.WRAP_CONTENT
//        params?.gravity = Gravity.BOTTOM
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        when (activity) {
            is JunkCleanActivity -> {
                binding.tvTitle.text = activity.getString(R.string.exit_junk_clean)
                binding.tvContent.text = activity.getString(R.string.exit_junk_clean_content)
            }

            is BatteryInfoActivity -> {
                binding.tvTitle.text = activity.getString(R.string.exit_battery_info)
                binding.tvContent.text = activity.getString(R.string.exit_battery_info_content)
            }

            is LargeFileCleanActivity -> {
                binding.tvTitle.text = activity.getString(R.string.exit_large_file_clean)
                binding.tvContent.text = activity.getString(R.string.exit_large_file_clean_content)
            }

            is PhotoCompressionActivity -> {
                binding.tvTitle.text = activity.getString(R.string.exit_photo_compression)
                binding.tvContent.text = activity.getString(R.string.exit_photo_compression_content)
            }

            is ScreenshotCleanActivity -> {
                binding.tvTitle.text = activity.getString(R.string.exit_screenshot_cleaner)
                binding.tvContent.text = activity.getString(R.string.exit_screenshot_cleaner_content)
            }

            is SimilarPhotosActivity -> {
                binding.tvTitle.text = activity.getString(R.string.exit_similar_photos)
                binding.tvContent.text = activity.getString(R.string.exit_similar_photos_content)
            }

            is AppUninstallActivity -> {
                binding.tvTitle.text = activity.getString(R.string.exit_uninstall_app)
                binding.tvContent.text = activity.getString(R.string.exit_uninstall_app_content)
            }

            else -> {
                back.invoke()
                return
            }
        }
        dialog.show()

        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)

        binding.ivCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvSure.setOnClickListener {
            dialog.dismiss()
            back.invoke()
        }
    }
}