package com.revoapp.calendar.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

import com.revoapp.api.Config;
import com.revoapp.api.database.calendar.CalendarRDH;
import com.revoapp.api.database.calendar.pojo.Events;
import com.revoapp.api.database.calendar.pojo.Pills;
import com.revoapp.api.helper.BroadcastHelper;
import com.revoapp.api.logger.RevoLogger;
import com.revoapp.calendar.CalendarActivity;
import com.revoapp.calendar.database.DatabaseHandler;
import com.revoapp.calendar.database.PillsDatabaseHandler;
import com.revoapp.calendar.database.PreferencesManager;
import com.revoapp.calendar.service.EventAlarmServices;
import com.revoapp.calendar.service.PillsAlarmServices;

import java.util.ArrayList;

public class Receiver extends BroadcastReceiver {
    private static boolean pillsDownloading = false;
    private static boolean eventsDownloading = false;
    private static boolean clearDataStarted = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        String intentAction = intent.getAction();
        RevoLogger.log(RevoLogger.VERBOSE, this.getClass().getName(),
                "onReceive", "action = " + intentAction);

        if (intentAction.equals(BroadcastHelper.ACTION_CALENDAR_UPDATE_EVENTS)) {
            if (!eventsDownloading) {
                new DownloadEvents(context).execute();
            }
        } else if (intentAction.equals(BroadcastHelper.ACTION_CALENDAR_UPDATE_PILLS)) {
            if (!pillsDownloading) {
                new DownloadPills(context).execute();
            }
        } else if (intentAction.equals(BroadcastHelper.ACTION_CLEAR_DATA)) {
            if (!clearDataStarted)
                new ClearDataAT(context).execute();
        }
    }

    private class DownloadEvents extends AsyncTask<Void, Void, Void> {
        private Context context;

        public DownloadEvents(Context context) {
            this.context = context;
            eventsDownloading = true;
        }

        @Override
        protected Void doInBackground(Void... v) {
            DatabaseHandler dh = new DatabaseHandler(context);
            dh.deleteAllEvents();

            CalendarRDH rdh = new CalendarRDH(context);
            ArrayList<Events> events = rdh.getEvents();
            dh.addEvents(events);

            Intent mServiceIntent = new Intent(context, EventAlarmServices.class);
            mServiceIntent.setAction(EventAlarmServices.CREATE);
            context.startService(mServiceIntent);

            return null;
        }

        @Override
        protected void onPostExecute(Void v) {
            CalendarActivity.refresh();
            eventsDownloading = false;
        }
    }

    private class DownloadPills extends AsyncTask<Void, Void, Void> {
        private Context context;

        public DownloadPills(Context context) {
            this.context = context;
            pillsDownloading = true;
        }

        @Override
        protected Void doInBackground(Void... v) {
            PillsDatabaseHandler pdh = new PillsDatabaseHandler(context);
            pdh.deleteAllPill();

            CalendarRDH rdh = new CalendarRDH(context);
            ArrayList<Pills> pills = rdh.getPills();
            pdh.addPills(pills);

            Intent mServiceIntent = new Intent(context, PillsAlarmServices.class);
            mServiceIntent.setAction(PillsAlarmServices.CREATE);
            context.startService(mServiceIntent);

            return null;
        }

        @Override
        protected void onPostExecute(Void v) {
            CalendarActivity.refresh();
            pillsDownloading = false;
        }
    }

    private class ClearDataAT extends AsyncTask<Void, Void, Void> {
        private Context context;

        public ClearDataAT(Context context) {
            this.context = context;
            clearDataStarted = true;
        }

        @Override
        protected Void doInBackground(Void... v) {
            PillsDatabaseHandler pdh = new PillsDatabaseHandler(context);
            pdh.dropDatabase();

            DatabaseHandler dh = new DatabaseHandler(context);
            dh.dropDatabase();

            PreferencesManager.clearSettings(context, Config.DATABASE_CALENDAR);

            return null;
        }

        @Override
        protected void onPostExecute(Void v) {
            CalendarActivity.refresh();
            clearDataStarted = false;
        }
    }
}