package com.mapotempo.fleet;

import com.couchbase.lite.Context;
import com.mapotempo.fleet.api.MapotempoFleetManagerInterface;
import com.mapotempo.fleet.core.DatabaseHandler;
import com.mapotempo.fleet.core.exception.CoreException;
import com.mapotempo.fleet.core.model.Company;
import com.mapotempo.fleet.core.model.User;
import com.mapotempo.fleet.core.model.accessor.CompanyAccess;
import com.mapotempo.fleet.core.model.accessor.MissionAccess;
import com.mapotempo.fleet.core.model.accessor.UserAccess;

import java.util.List;

/**
 * MapotempoFleetManager.
 */
public class MapotempoFleetManager implements MapotempoFleetManagerInterface {

    private Context mContext;

    private DatabaseHandler mDatabaseHandler;

    private CompanyAccess mCompanyAccess;

    private UserAccess mUserAccess;

    private MissionAccess mMissionAccess;

    /**
     * {@inheritDoc}
     */
    @Override
    public Company getCompany() {
        List<Company> companies = mCompanyAccess.getAll();
        if(companies.size() > 0)
            return companies.get(0);
        else
            return mCompanyAccess.getNew();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public User getUser() {
            List<User> users = mUserAccess.getAll();
        if(users.size() > 0)
            return users.get(0);
        else
            return mUserAccess.getNew();
  }

    /**
     * {@inheritDoc}
     */
    @Override
    public MissionAccess getMissionAccess() {
        return mMissionAccess;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean connexion(boolean status) {
        // TODO
        System.err.println("connexion not implemented");
        return false;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean getConnexionStatus() {
        // TODO
        System.err.println("connexion not implemented");
        return false;
    }

    /**
     * todo
     * @param context
     * @return return a {@link MapotempoFleetManagerInterface}
     */
    public static MapotempoFleetManagerInterface getDefaultManager(Context context) {
        return new MapotempoFleetManager(context);
    }

    /**
     * Default manager.
     * @param context java context
     */
    private MapotempoFleetManager(Context context) {
        mContext = context;
        try {
            mDatabaseHandler = new DatabaseHandler("default", mContext);
            mMissionAccess = new MissionAccess(mDatabaseHandler);
            mCompanyAccess = new CompanyAccess(mDatabaseHandler);
            mUserAccess = new UserAccess(mDatabaseHandler);
        } catch (CoreException e) {
            e.printStackTrace();
        };
    }

    /**
     * todo
     * @param context
     * @param user
     * @param password
     * @return return a {@link MapotempoFleetManagerInterface}
     */
    public static MapotempoFleetManagerInterface getManager(Context context, String user, String password) {
        return new MapotempoFleetManager(context, user, password);
    }

    /**
     * Connected manager.
     * @param context java context
     * @param user user login
     * @param password user password
     */
    private MapotempoFleetManager(Context context, String user, String password) {
        mContext = context;
        try {
            mDatabaseHandler = new DatabaseHandler(user, mContext);
            mDatabaseHandler.setConnexionParam(password, "http://localhost:4985/db");
            mMissionAccess = new MissionAccess(mDatabaseHandler);
            mCompanyAccess = new CompanyAccess(mDatabaseHandler);
            mUserAccess = new UserAccess(mDatabaseHandler);
        } catch (CoreException e) {
            e.printStackTrace();
        };
    }
}

@@ -3720,10 +3720,1115 @@
 ;%0A    %7D%0A
+%0A%0A    /**%0A     * todo%0A     * @param context%0A     * @param user%0A     * @param url server url%0A     * @param password%0A     * @return return a %7B@link MapotempoFleetManagerInterface%7D%0A     */%0A    public static MapotempoFleetManagerInterface getManager(Context context, String user, String password, String url) %7B%0A        return new MapotempoFleetManager(context, user, password, url);%0A    %7D%0A%0A    /**%0A     * Connected manager.%0A     * @param context java context%0A     * @param user user login%0A     * @param url server url%0A     * @param password user password%0A     */%0A    private MapotempoFleetManager(Context context, String user, String password, String url) %7B%0A        mContext = context;%0A        try %7B%0A            mDatabaseHandler = new DatabaseHandler(user, mContext);%0A            mDatabaseHandler.setConnexionParam(password, url);%0A            mMissionAccess = new MissionAccess(mDatabaseHandler);%0A            mCompanyAccess = new CompanyAccess(mDatabaseHandler);%0A            mUserAccess = new UserAccess(mDatabaseHandler);%0A        %7D catch (CoreException e) %7B%0A            e.printStackTrace();%0A        %7D;%0A    %7D%0A
 %7D%0A

Add MapotempoFleetMananger with url param.