package com.xypower.mpapp;

import static android.content.Context.ALARM_SERVICE;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.util.Log;

import java.util.Date;
import java.util.List;

public class AlarmUtil {

    public final static String TAG = "MPLOG";
    public final static long MASK_CHANNEL = 0xFF00L;
    public final static long MASK_PRESET = 0xFF0000L;
    public final static long MASK_SCHEDULE_TIME = 0xFFFFFF000000L;

    public final static int SHIFT_CHANNEL = 8;
    public final static int SHIFT_PRESET = 16;
    public final static int SHIFT_SCHEDULE_TIME = 24;

    private static final String ACTION_TAKE_PHOTO = "ACT_TP";

    private static final String EXTRA_PARAM_CHANNEL = "Channel";
    private static final String EXTRA_PARAM_PRESET = "Preset";
    private static final String EXTRA_PARAM_PHOTO_OR_VIDEO = "PhotoOrVideo";
    private static final String EXTRA_PARAM_SCHEDULES = "Schedules";
    private static final String EXTRA_PARAM_SCHEDULE = "Schedule_";

    private static final String EXTRA_PARAM_TIME = "Time";

    public static void registerPhotoTimer(Context context, int channel, int preset, long ts, long timeout, List<Long> schedules) {

        // 创建延迟意图
        Intent alarmIntent = new Intent();
        alarmIntent.setAction(ACTION_TAKE_PHOTO);
        int cnt = schedules.size();
        alarmIntent.putExtra(EXTRA_PARAM_SCHEDULES, cnt);
        String channelStr = "";
        for (int idx = 0; idx < cnt; idx++) {
            long val = schedules.get(idx).longValue();
            alarmIntent.putExtra(EXTRA_PARAM_SCHEDULE + idx, schedules.get(idx).longValue());
            channelStr += "CH=" + ((val & 0XFF0000) >> 16) + "-PR=" + ((val & 0XFF00) >> 8) + " ";
        }

        alarmIntent.putExtra(EXTRA_PARAM_TIME, ts);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        long currentTimeMillis = System.currentTimeMillis();
        Date date = new Date(currentTimeMillis + timeout);
        String dateStr = (String) DateFormat.format("MM-dd kk:mm:ss", date);
        Log.d(TAG, "PhotoTimer Reg: " + dateStr + " currentTimeMillis=" + currentTimeMillis + " timeout=" + timeout + " Channels=" + channelStr);

        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent);
    }

    public static boolean parseTakingPhotoAlarm(Intent intent) {

        long ts = intent.getLongExtra(EXTRA_PARAM_TIME, 0);
        int cnt = intent.getIntExtra(EXTRA_PARAM_SCHEDULES, 0);
        if (cnt > 0) {
            for (int idx = 0; idx < cnt; idx++) {
                long val = intent.getLongExtra(EXTRA_PARAM_SCHEDULE + idx, 0);

                int channel = (int) ((val & 0xFF0000L) >> 16);
                int preset = (int) ((val & 0xFF00L) >> 8);

                Log.i(TAG, "PhotoTimer Fired: CH=" + channel + " PR=" + preset);
                // mService.notifyToTakePhoto(mService.mNativeHandle, channel, preset, ts, mService.buildPhotoDir(mService.getApplicationContext(), channel), mService.buildPhotoFileName(channel, preset, ts), true);
            }
        }

        // Register Next Photo Timer
        Date date = new Date();
        long nowTs = date.getTime() / 1000;
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        long startTime = date.getTime() / 1000;
        long baseTime = nowTs - startTime;

        return true;
    }


}
