package com.develop.setting.ui

import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import android.view.LayoutInflater
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.base.ext.updateText
import com.develop.common.data_repo.FoodDataProvider
import com.develop.common.router.Screens
import com.develop.common.ui.CommonBindingActivity
import com.develop.common.dialog.CancelConfirmDialog
import com.develop.setting.databinding.ActivityRestoreBinding
import java.io.File

@Route(path = Screens.Setting.RESTORE)
class RestoreActivity : CommonBindingActivity<ActivityRestoreBinding>() {
    override fun createViewBinding(inflater: LayoutInflater): ActivityRestoreBinding {
        return ActivityRestoreBinding.inflate(layoutInflater)
    }

    private val dialog = CancelConfirmDialog()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.tvTitle.updateText(getString(com.develop.common.R.string.restore_factory_settings))
        binding.ivClose.setOnClickListener {
            finish()
        }
        dialog.title = getString(com.develop.common.R.string.restore_confirm_tips)
        dialog.onDialogClickListener = object : CancelConfirmDialog.OnDialogClickListener {
            override fun onConfirm() {
                FoodDataProvider.deleteAll()
                val cacheDirectory = cacheDir
                val applicationDirectory = cacheDirectory.parent?.let { File(it) }
                if (applicationDirectory?.exists() == true) {

                    val files = applicationDirectory.listFiles() ?: emptyArray()
                    for (file in files) {
                        if (!file.name.equals("lib")) {
                            com.blankj.utilcode.util.FileUtils.delete(file)
                        }
                    }
                }
                val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
                pm.reboot("")
            }

            override fun onCancel() {
            }

        }
        binding.tvYea.setOnClickListener {
            dialog.showDialog(supportFragmentManager, "confirm")
        }
        binding.tvNo.setOnClickListener {
            finish()
        }

    }
}