package com.tcl.factory.utils;

import android.annotation.RequiresPermission;
import android.app.AlarmManager;
import android.content.Context;
import android.os.IRtcService;
import android.os.ServiceManager;
import android.util.Log;

import java.util.Calendar;
import java.util.TimeZone;

public class RtcTimeHelper {
	private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    private int second;
    private Calendar mCalendar;
    private Context myContext;
    private String TAG = RtcTimeHelper.class.getName();

    public RtcTimeHelper(Context context) {
        this.myContext = context;
    }
    
    public void setData(int year, int month, int day, int hour,int minute, int second) {
    	this.year = year;
    	this.month = month;
    	this.day = day;
    	this.hour = hour;
    	this.minute = minute;
    	this.second = second;
    }

    public boolean parseTime(String dateStr, String timeStr) {
        if (dateStr == null || dateStr.length() != 8) {
            return false;
        }
        if (timeStr == null || timeStr.trim().length() != 6) {
            return false;
        }
        Log.i(TAG, "dateStr:" + dateStr);
        Log.i(TAG, "timeStr:" + timeStr);
        try {
            year = Integer.parseInt(dateStr.substring(0, 4));
            month = Integer.parseInt(dateStr.substring(4, 6));
            day = Integer.parseInt(dateStr.substring(6, 8));
            hour = Integer.parseInt(timeStr.substring(0, 2));
            minute = Integer.parseInt(timeStr.substring(2, 4));
            second = Integer.parseInt(timeStr.substring(4, 6));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean isValid() {
        if (month > 12 || month == 0 || day == 0) {
            return false;
        }
        boolean isRun = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
        if (month == 2) {
            if (isRun) {
                if (day > 28) {
                    return false;
                }
            } else {
                if (day > 29) {
                    return false;
                }
            }
        }
        if (month <= 7) {
            if (month % 2 == 1) {
                if (day > 31) return false;
            } else {
                if (day > 30) return false;
            }
        } else {
            if (month % 2 == 0) {
                if (day > 31) return false;
            } else {
                if (day > 30) return false;
            }
        }
        if (hour >= 24) {
            return false;
        }
        if (minute >= 60) {
            return false;
        }
        if (second >= 60) {
            return false;
        }
        return true;
    }

    public boolean setTime() {
    	mCalendar = Calendar.getInstance();
	    Log.i("time_set","set time:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
	    mCalendar.set(year,month-1,day,hour,minute,second);
	    long time =mCalendar.getTimeInMillis()- TimeZone.getDefault().getRawOffset();
	    mCalendar.setTimeInMillis(time);
	    int wDay = mCalendar.get(Calendar.DAY_OF_WEEK);
	    final String SERVICE_RTC="rtc";
	    try {
	        IRtcService iRtcService = IRtcService.Stub.asInterface(ServiceManager.getService(SERVICE_RTC));
	        int ret = iRtcService.setRTCSysTime(mCalendar.get(Calendar.YEAR),
	                mCalendar.get(Calendar.MONTH)+1,mCalendar.get(Calendar.DAY_OF_MONTH),
	                mCalendar.get(Calendar.DAY_OF_WEEK),mCalendar.get(Calendar.HOUR_OF_DAY),
	                mCalendar.get(Calendar.MINUTE),mCalendar.get(Calendar.SECOND));
	        if(ret== 0){
	            double retTime = iRtcService.getRTCSysTime();
	            Log.i("time_set","set time---->"+time);
	            return true ;
	       }
	    }catch (Exception e){
	        e.printStackTrace();
	    }
	     return false ;
    }
    
    public boolean setRtcWakeup() {
    	mCalendar = Calendar.getInstance();
        Log.i(TAG,"set time:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
        mCalendar.set(year,month-1,day,hour,minute,second);
        long time =mCalendar.getTimeInMillis()- TimeZone.getDefault().getRawOffset();
        mCalendar.setTimeInMillis(time);
        int wDay = mCalendar.get(Calendar.DAY_OF_WEEK);
        final String SERVICE_RTC="rtc";
        try {
            IRtcService iRtcService = IRtcService.Stub.asInterface(ServiceManager.getService(SERVICE_RTC));
            int ret = iRtcService.setAlarmWakeupTime(mCalendar.get(Calendar.YEAR),
                    mCalendar.get(Calendar.MONTH)+1,mCalendar.get(Calendar.DAY_OF_MONTH),
                    mCalendar.get(Calendar.DAY_OF_WEEK),mCalendar.get(Calendar.HOUR_OF_DAY),
                    mCalendar.get(Calendar.MINUTE),mCalendar.get(Calendar.SECOND));
            if(ret== 0){
                double retTime = iRtcService.getRTCSysTime();
                Log.i(TAG,"set time---->"+time);
                return true ;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false ;
    }
    
    private boolean setTime(int year,int month,int day,int  hour, int minute, int  second){
    	mCalendar = Calendar.getInstance();
       Log.i("time_set","set time:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
       mCalendar.set(year,month-1,day,hour,minute,second);
       long time =mCalendar.getTimeInMillis()- TimeZone.getDefault().getRawOffset();
       mCalendar.setTimeInMillis(time);
       int wDay = mCalendar.get(Calendar.DAY_OF_WEEK);
       final String SERVICE_RTC="rtc";
       try {
           IRtcService iRtcService = IRtcService.Stub.asInterface(ServiceManager.getService(SERVICE_RTC));
           int ret = iRtcService.setRTCSysTime(mCalendar.get(Calendar.YEAR),
                   mCalendar.get(Calendar.MONTH)+1,mCalendar.get(Calendar.DAY_OF_MONTH),
                   mCalendar.get(Calendar.DAY_OF_WEEK),mCalendar.get(Calendar.HOUR_OF_DAY),
                   mCalendar.get(Calendar.MINUTE),mCalendar.get(Calendar.SECOND));
           if(ret== 0){
               double retTime = iRtcService.getRTCSysTime();
               Log.i("time_set","set time---->"+time);
               return true ;
           }
       }catch (Exception e){
           e.printStackTrace();
       }
       return false ;
   }
    
    private boolean setAlarmTime(int year,int month,int day,int  hour, int minute, int  second){
  	  mCalendar = Calendar.getInstance();
      Log.i("time_set","set time:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
      mCalendar.set(year,month-1,day,hour,minute,second);
      long time =mCalendar.getTimeInMillis()- TimeZone.getDefault().getRawOffset();
      mCalendar.setTimeInMillis(time);
      int wDay = mCalendar.get(Calendar.DAY_OF_WEEK);
      final String SERVICE_RTC="rtc";
      try {
          IRtcService iRtcService = IRtcService.Stub.asInterface(ServiceManager.getService(SERVICE_RTC));
          int ret = iRtcService.setAlarmWakeupTime(mCalendar.get(Calendar.YEAR),
                  mCalendar.get(Calendar.MONTH)+1,mCalendar.get(Calendar.DAY_OF_MONTH),
                  mCalendar.get(Calendar.DAY_OF_WEEK),mCalendar.get(Calendar.HOUR_OF_DAY),
                  mCalendar.get(Calendar.MINUTE),mCalendar.get(Calendar.SECOND));
          if(ret== 0){
              double retTime = iRtcService.getRTCSysTime();
              Log.i("time_set","set time---->"+time);
              return true ;
          }
      }catch (Exception e){
          e.printStackTrace();
      }
      return false ;
  }
    
    public double getAlarmTime() {
    	final String SERVICE_RTC="rtc";
        try {
            IRtcService iRtcService = IRtcService.Stub.asInterface(ServiceManager.getService(SERVICE_RTC));
            double ret = iRtcService.getAlarmWakeupTime();
            Log.i("time_set","set time---->"+ret);
            return ret ;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }
    
    public double getRtcTime() {
    	final String SERVICE_RTC="rtc";
        try {
            IRtcService iRtcService = IRtcService.Stub.asInterface(ServiceManager.getService(SERVICE_RTC));
            double ret = iRtcService.getRTCSysTime();
            Log.i("time_set","set time---->"+ret);
            return ret ;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    public  int disableRtcWakeup() {
        final String SERVICE_RTC = "rtc";
        try {
            IRtcService iRtcService = IRtcService.Stub.asInterface(ServiceManager.getService(SERVICE_RTC));
            return iRtcService.closeAlarm();
        } catch (Exception e) {
            e.printStackTrace();
        }
    	return -1;
    }
    
    @RequiresPermission(allOf = {"android.permission.WRITE_SETTINGS", "android.permission.WRITE_SECURE_SETTINGS"})
    public void setSysDateTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);
        c.set(Calendar.MILLISECOND, 0);
        long when = c.getTimeInMillis();
        if (when / 1000 < Integer.MAX_VALUE) {
            ((AlarmManager) myContext.getSystemService(Context.ALARM_SERVICE)).setTime(when);
            Log.i(TAG, "start set date");
        }
    }
}
