package com.develop.setting.init.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.food.base.manager.wifi.WifiHelp
import com.develop.food.base.repo.FoodDataProvider
import com.develop.food.base.router.Screens
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.widgets.CancelConfirmDialog
import com.develop.setting.R
import com.develop.setting.databinding.ActivityRestoreBinding
import java.io.File

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.topBar.setTopBarTitle(getString(R.string.restore_factory_settings))
        binding.topBar.listenBackClick {
            finish()
        }

        binding.tvYea.setOnClickListener {

           var dailog =  CancelConfirmDialog()
            dailog.title = getString(R.string.restore_confirm_tips)

            dailog.onDialogClickListener = object:CancelConfirmDialog.OnDialogClickListener {
                override fun onConfirm() {
                    FoodDataProvider.deleteAll()
                    WifiHelp.Build(this@RestoreActivity).build().removeAllWifi()
                    var cacheDirectory = getCacheDir();
                    var applicationDirectory =  File(cacheDirectory.getParent());
                    if (applicationDirectory.exists()) {

                        var files = applicationDirectory.listFiles();

                        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() {
                }

            }

            dailog.showDialog(supportFragmentManager,"confirm")


        }

    }
}