package de.slgdev.leoapp;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;

import java.util.ArrayList;

import de.slgdev.leoapp.service.AlarmStartupService;
import de.slgdev.leoapp.service.ReceiveService;
import de.slgdev.leoapp.task.MailSendTask;
import de.slgdev.leoapp.task.SyncFilesTask;
import de.slgdev.leoapp.task.SyncGradeTask;
import de.slgdev.leoapp.task.SyncUserTask;
import de.slgdev.leoapp.utility.User;
import de.slgdev.leoapp.utility.Utils;
import de.slgdev.schwarzes_brett.task.UpdateViewTrackerTask;
import de.slgdev.schwarzes_brett.utility.SchwarzesBrettUtils;
import de.slgdev.startseite.activity.MainActivity;
import de.slgdev.stimmungsbarometer.task.SyncQuestionTask;
import de.slgdev.stimmungsbarometer.task.SyncVoteTask;

public class Start extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Utils.getController().closeActivities();
        Utils.getController().closeServices();
        Utils.getController().closeDatabases();

        Utils.getController().setContext(getApplicationContext());

        if (Utils.isVerified()) {
            runUpdateTasks();
        }

        startServices();

        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        finish();
    }

    public static void runUpdateTasks() {
        if (Utils.isNetworkAvailable()) {
 //           new SyncUserTask().execute();

    /*        if (Utils.getUserPermission() != User.PERMISSION_LEHRER)
                new SyncGradeTask().execute();
            else
                Utils.getController().getPreferences()
                        .edit()
                        .putString("pref_key_general_klasse", "TEA")
                        .apply();
*/
            //TODO DEBUG vvvvvv

            Utils.getController().getPreferences()
                    .edit()
                    .putString("pref_key_general_klasse", "05f")
                    .apply();

            //TODO ENTFERNEN ^^^^

            new SyncQuestionTask().execute();
            new SyncVoteTask().execute();
            new SyncFilesTask().execute();

            ArrayList<Integer> cachedViews = SchwarzesBrettUtils.getCachedIDs();
            new UpdateViewTrackerTask().execute(cachedViews.toArray(new Integer[cachedViews.size()]));

            if (!Utils.getController().getPreferences().getString("pref_key_request_cached", "-").equals("-")) {
                new MailSendTask().execute(Utils.getController().getPreferences().getString("pref_key_request_cached", ""));
            }
        }
    }

    private void startServices() {
        if (Utils.isVerified()) {
            startReceiveService();
            startService(new Intent(getApplicationContext(), AlarmStartupService.class));
            initSyncAdapter();
        }
    }

    public static void startReceiveService() {
        if (Utils.isNetworkAvailable()) {
            Utils.getContext().startService(new Intent(Utils.getContext(), ReceiveService.class));
        }
    }

    private void initSyncAdapter() {
        ContentResolver.addPeriodicSync(
                createSyncAccount(),
                "de.slgdev.leoapp",
                Bundle.EMPTY,
                10 * 60
        );
    }

    private Account createSyncAccount() {
        AccountManager am = AccountManager.get(this);
        Account[]      accounts;

        try {
            accounts = am.getAccountsByType("de.slgdev.leoapp");
        } catch (SecurityException e) {
            accounts = new Account[0];
        }

        if (accounts.length > 0) {
            return accounts[0];
        }

        Account newAccount = new Account("default_account", "de.slgdev.leoapp");

        if (am.addAccountExplicitly(newAccount, "pass1", null)) {
            ContentResolver.setIsSyncable(newAccount, "de.slgdev.leoapp", 1);
            ContentResolver.setSyncAutomatically(newAccount, "de.slgdev.leoapp", true);
        } else {
            newAccount = null;
        }

        return newAccount;
    }
}
@@ -1549,18 +1549,16 @@
 e()) %7B%0A 
-//
         
@@ -1599,10 +1599,8 @@
     
-/*
     
@@ -1917,253 +1917,8 @@
 y();
-%0A*/%0A            //TODO DEBUG vvvvvv%0A%0A            Utils.getController().getPreferences()%0A                    .edit()%0A                    .putString(%22pref_key_general_klasse%22, %2205f%22)%0A                    .apply();%0A%0A            //TODO ENTFERNEN %5E%5E%5E%5E
 %0A%0A  

Debug entfernt