package com.rthoni.camotion.business;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

import com.luticate.utils.dbo.LuDataAccessConfigDbo;
import com.rthoni.camotion.R;
import com.rthoni.camotion.dbo.LocationDbo;

import java.util.List;
import java.util.Vector;

/**
 *
 * Created by robin on 11/28/15.
 */
public class CamotionBusiness {

    public static LuDataAccessConfigDbo getConfig(LocationDbo location)
    {
        LuDataAccessConfigDbo config = new LuDataAccessConfigDbo();
        config.setBaseUrl(location.getBaseUrl());
        return config;
    }

    public static List<LocationDbo> getLocations(Context context)
    {
        List<LocationDbo> locations = new Vector<>();
        AccountManager accountManager = AccountManager.get(context);
        for (Account account : accountManager.getAccountsByType(context.getString(R.string.account_type))) {
            LocationDbo location = new LocationDbo();
            String[] split = account.name.split("@");

            location.setPassword(accountManager.getPassword(account));
            location.setHttps(accountManager.getUserData(account, "Https").equals("true"));
            location.setDomain(accountManager.getUserData(account, "Domain"));
            location.setUsername(split[0]);
            location.setName(split[1]);

            locations.add(location);
        }
        return locations;
    }
}
