package com.easy.clean.ui.dialog

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import com.easy.clean.R
import com.easy.clean.bean.config.AdConfigBean
import com.easy.clean.business.ads.AdsMgr
import com.easy.clean.databinding.DialogFunctionBackBinding
import com.easy.clean.ui.batteryinfo.BatteryInfoActivity
import com.easy.clean.ui.clean.JunkCleanActivity
import com.easy.clean.ui.largefile.LargeFileCleanActivity
import com.easy.clean.ui.photocompression.PhotoCompressionActivity
import com.easy.clean.ui.screenshot.ScreenshotCleanActivity
import com.easy.clean.ui.similar.SimilarPhotosActivity
import com.easy.clean.ui.videoclean.VideoCleanActivity

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_cleaner_content)
            }
            is VideoCleanActivity->{
                binding.tvTitle.text = activity.getString(R.string.Exit_Video_Clean)
                binding.tvContent.text = activity.getString(R.string.Exit_Video_Clean_Content)
            }

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

        if (AdConfigBean.adsConfigBean.isAdShow){
            AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom_r16)
        }else{
            binding.clAd.visibility= View.GONE
        }

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