package com.base.filerecoveryrecyclebin.view

import android.animation.ValueAnimator
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View.OnClickListener
import android.view.WindowManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.base.filerecoveryrecyclebin.activity.junkclean.ScanJunk2Activity
import com.base.filerecoveryrecyclebin.activity.junkclean.ScanJunkActivity
import com.base.filerecoveryrecyclebin.databinding.DialogCleanGuideBinding
import com.base.filerecoveryrecyclebin.utils.AppPreferences

class CleanGuideDialog(
    context: Context,
    private val binding: DialogCleanGuideBinding) : Dialog(context),
    DefaultLifecycleObserver {
    override fun onCreate(savedInstanceState: Bundle?) {
        super<Dialog>.onCreate(savedInstanceState)
        init()
    }

    override fun onDestroy(owner: LifecycleOwner) {
        cancel()
        super.onDestroy(owner)
    }

    private fun gotoScan(){
        val jumpJunkFirstAd = AppPreferences.getInstance().getString("isjunkPlayAd", "0").toInt()
        if (jumpJunkFirstAd == 1) {
            context.startActivity(Intent(context, ScanJunk2Activity::class.java))
        } else {
            context.startActivity(Intent(context, ScanJunkActivity::class.java))
        }
        cancel()
    }
    private fun init() {
        binding.tvBtn.setOnClickListener {
            gotoScan()
        }
        binding.lottie.repeatCount = ValueAnimator.INFINITE
        binding.lottie.setOnClickListener {
            gotoScan()
        }
        setCancelable(false)
        setCanceledOnTouchOutside(false)
        setContentView(binding.root)
        window?.run {
            setBackgroundDrawable(ColorDrawable())
            setDimAmount(0.7f)
            setGravity(Gravity.TOP)
            val layoutParams = attributes
            //设置dialog可以穿透并且有蒙层
            layoutParams.flags =
                    //  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                WindowManager.LayoutParams.FLAG_DIM_BEHIND
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
//            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            // layoutParams.verticalMargin = 0.06f
            attributes = layoutParams
            decorView.setPadding(0, 0, 0, 0)
        }
    }

    class Builder(private val context: Context) {
        private lateinit var dialog: CleanGuideDialog
        private val binding by lazy(LazyThreadSafetyMode.NONE) {
            DialogCleanGuideBinding.inflate(LayoutInflater.from(context))
        }

        fun build(lifecycleOwner: LifecycleOwner? = null): CleanGuideDialog =
            CleanGuideDialog(context, binding).also {
                dialog = it
                lifecycleOwner?.lifecycle?.addObserver(it)
            }
    }
}