package kz.arta.synergy.workflow.mobile.android;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

import com.devspark.appmsg.AppMsg;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.authorization.SynergyAccount;
import kz.arta.synergy.workflow.mobile.android.common.models.Person;
import kz.arta.synergy.workflow.mobile.android.common.offline.add.AddSync;
import kz.arta.synergy.workflow.mobile.android.common.utils.MyApplication;
import kz.arta.synergy.workflow.mobile.android.common.utils.Utils;

/**
 * Created by neox on 14.08.14.
 * Оповещение о подключении/отключении интернета
 */
public class NetworkBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Activity currentActivity = ((MyApplication)context.getApplicationContext()).getCurrentActivity();
        if (currentActivity != null) {
            if (Utils.isOnline(context, null)) {
                AppMsg.makeText(currentActivity, context.getString(R.string.internet_enabled), AppMsg.GRAY_STYLE, R.layout.internet_enabled).show();
                Sync sync = new Sync(context);
                sync.execute();
            } else {
                AppMsg.makeText(currentActivity, context.getString(R.string.internet_disabled), AppMsg.GRAY_STYLE, R.layout.internet_disabled).show();
            }
        }
    }

    class Sync extends AsyncTask<Void, Void, Void> {

        Context context;

        Person person;

        Sync(Context context) {
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            AccountManager accountManager = AccountManager.get(context);
            Account[] accounts = accountManager.getAccounts();
            for (Account account : accounts) {
                if (account.type.equals(SynergyAccount.TYPE)) {
                    person = new Person();
                    person.setPassword(accountManager.getPassword(account));
                    person.setLogin(accountManager.getUserData(account, SynergyAccount.LOGIN));
                    person.setHost(accountManager.getUserData(account, SynergyAccount.HOST));
                    person.setShortHost(accountManager.getUserData(account, SynergyAccount.SHORT_HOST));
                    person.setUuid(accountManager.getUserData(account, SynergyAccount.USER_ID));
                    person.setFirstname(accountManager.getUserData(account, SynergyAccount.FIRSTNAME));
                    person.setLastname(accountManager.getUserData(account, SynergyAccount.LASTNAME));
                    break;
                }
            }
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            AddSync addSync = new AddSync(context, person);
            addSync.start();
            addSync.clear();
            return null;
        }
    }
}