package com.wdkl.ncs.s433.transbox.utils;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.wdkl.ncs.s433.transbox.Application;
import com.wdkl.ncs.s433.transbox.MainActivity;

import java.util.Calendar;

/**
 * Created by dengzhe on 2018/4/18.
 * //========自动重启工具类========//
 */

public class AutoRebootUtil {
    private static Context context;

    private static Calendar calendar;
    private static int countDownMinute;
    private static String currentTime = "0";
    private static int timeFirst = 11;
    private static int timeSecond = 17;
    private static int mt = 50;
    private static int lt = 55;
    private static int hour;
    private static int minute;


    public static boolean sys_Reset = false;
    public static boolean sys_Shutdown = false;

    public static void reboot(Context context) {
        if (!sys_Reset) {
            sys_Reset = true;
            Intent intent = new Intent(Intent.ACTION_REBOOT);
            intent.putExtra("nowait", 1);
            intent.putExtra("interval", 1);
            intent.putExtra("window", 0);
            context.sendBroadcast(intent);
        }
    }

    public static void shutdown(Context context) {
        if (!sys_Shutdown) {
            sys_Shutdown = true;
            Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
            intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }


    public static void reboot() {
        if (!sys_Reset) {
            sys_Reset = true;
            try {
                Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot "});
                proc.waitFor();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void shutdown() {
        if (!sys_Shutdown) {
            sys_Shutdown = true;
            try {
                Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot -p"});
                proc.waitFor();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private static boolean restart = false;
    public static void restartApp() {
        if (!restart) {
            try {
                Intent intent = new Intent(Application.getContext(), MainActivity.class);
                @SuppressLint("WrongConstant") PendingIntent mPendingIntent = PendingIntent.getActivity(
                        Application.getContext(), 0, intent,
                        Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                AlarmManager mgr = (AlarmManager) Application.getContext().getSystemService(Context.ALARM_SERVICE);
                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, mPendingIntent);

            } catch (Exception e) {
                e.printStackTrace();
            }

            restart = true;
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }
}
