package com.wdkl.ncs.android.middleware.utils;

import android.app.AlarmManager;
import android.content.Context;
import android.text.TextUtils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;

public class AppUtil {

    /**
     * 设置系统时间
     *
     * @param time
     */
    public static void setSysTime(String time, String timeZone) {
        try {
            Process process = Runtime.getRuntime().exec("su");
            if (null == process) return;
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            //os.writeBytes("setprop persist.sys.timezone Asia/Shanghai\n");
            os.writeBytes("setprop persist.sys.timezone " + timeZone + "\n");
            if (android.os.Build.VERSION.SDK_INT >= 24) {//7.1以上的系统
                String datetime = changeTimeForm(time); //20211213:092314  ------  051315372019.00
                os.writeBytes("/system/bin/date " + datetime + " set\n");
            } else {
                os.writeBytes("/system/bin/date -s " + time + "\n");//【时间格式 yyyyMMdd.HHmmss】"20131023.112800"
            }
            os.writeBytes("clock -w\n");
            os.writeBytes("exit\n");
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String changeTimeForm(String t) {
        if (!TextUtils.isEmpty(t) && t.length() >= 15) {
            String yyyy = substringByLengh(t, 0, 4);
            String MMdd = substringByLengh(t, 4, 8);
            String HHmm = substringByLengh(t, 9, 13);
            String ss = substringByLengh(t, 13, 15);

            return MMdd + HHmm + yyyy + "." + ss;
        } else {
            return "051315372019.00";
        }
    }

    /**
     * 字符串按索引截取
     *
     * @param str
     * @return
     */
    public static String substringByLengh(String str, int start, int end) {
        if (str == null) {
            return "";
        }
        if (start > end) {
            return "";
        }
        if (str.length() - 1 < start || str.length() < end) {
            return "";
        }

        return str.substring(start, end);
    }


    public static void setSystemTime(Context context, int year, int month, int day, int hour, int minute, int mill) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, mill);
        c.set(Calendar.MILLISECOND, 0);
        long when = c.getTimeInMillis();
        if (when / 1000 < Integer.MAX_VALUE) {
            ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
        }
    }

    public static void setSystemTime(Context context, long timeMills, String timeZone) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setTimeZone(timeZone);
        alarmManager.setTime(timeMills);
    }
}
