package com.tofly.ghrq_route.module.user.controller;

/**
 * Created by Amu on 2017/2/9.
 */

import android.content.Context;

import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.module.user.domain.CompanyBean;
import com.tofly.ghrq_route.module.user.domain.CompanyUserBean;
import com.tofly.ghrq_route.module.user.domain.DeviceBean;
import com.tofly.ghrq_route.module.user.service.IUserService;
import com.tofly.ghrq_route.module.user.service.UserServiceImpl;
import com.tofly.ghrq_route.module.user.service.UserServiceImplOffline;

/**
 * 用户控制类
 */
public class UserController {

    /**
     * 是否在綫
     */
    boolean mbOnline;
    /**
     * 上下文
     */
    Context mContext;
    /**
     * User接口实现对象
     */
    IUserService userService;

    public UserController(Context context){
        mbOnline = true;
        mContext = context;
    }

    public UserController(boolean online, Context context){
        mbOnline = online;
        mContext = context;
        userService = new UserServiceImpl(context);
    }

    private void setUserService(){
        boolean isOffline = OfflineWorkTool.isOffline();
        if (isOffline){
            userService = new UserServiceImplOffline(mContext);
        }else {
            userService = new UserServiceImpl(mContext);
        }
    }

    /**
     * 登录方法
     * @param username 用户名
     * @param token 密码
     * @param userCallBack 函数回调
     */

    public void loginOnline(String username, String token, boolean force, final UserCallBack userCallBack) {
        // TODO Auto-generated method stub
        userService = new UserServiceImpl(mContext);
        userService.login(username, token, force, userCallBack);
    }

    public static final int LOGIN_TEST_SERVER = 0;
    public static final int LOGIN_ONLINE = 1;
    public static final int LOGIN_OFFLINE = 2;
    public static final int LOGIN_UNCERTAIN = 3;

    public void login(String username, String token, boolean force, int login_mode,final UserCallBack userCallBack) {
        // TODO Auto-generated method stub
        userService = new UserServiceImpl(mContext);
        if (login_mode == LOGIN_OFFLINE){
            userService = new UserServiceImplOffline(mContext);
        }else if (login_mode == LOGIN_UNCERTAIN){
            userService = BaseApp.isNetworkConnected() ? new UserServiceImpl(mContext) : new UserServiceImplOffline(mContext);
        }
        userService.login(username, token, force, userCallBack);
    }


    /**
     *  获取登录账号信息
     * @param username 用户名
     * @param token
     * @param userCallBack
     */
    public void getAccountInfo(String username, String token, int login_mode, final UserCallBack userCallBack){
        userService = new UserServiceImpl(mContext);
        if (login_mode == LOGIN_OFFLINE){
            userService = new UserServiceImplOffline(mContext);
        }else if (login_mode == LOGIN_UNCERTAIN){
            userService = BaseApp.isNetworkConnected() ? new UserServiceImpl(mContext) : new UserServiceImplOffline(mContext);
        }
        userService.getAccountInfo(username, token, userCallBack);
    }

    /**
     *  获取用户功能权限列表
     */
    public void getUserMenus(Context context, final UserCallBack userCallBack) {
        setUserService();
        userService.getUserMenus(context, userCallBack);
    }

    /**
     * 更新用户信息
     * @param username 用户名
     * @param mobile 手机号
     * @param password 密码
     * @param avatar 头像
     * @param userCallBack 函数回调
     */

    public void updateAccountInfo(String userid, String username, String mobile, String password, String avatar, final UserCallBack userCallBack) {
        // TODO Auto-generated method stub
        setUserService();
        userService.updateAccountInfo(userid, username, mobile, password, avatar, userCallBack);
    }

    /**
     *  获取其他用户账号信息
     * @param userid 用户id
     * @param token
     * @param userCallBack
     */
    public void getUserInfo(String userid, String token, final UserCallBack userCallBack){
        setUserService();
        userService.getUserInfo(userid, token, userCallBack);
    }

    /**
     * 登录方法
     * @param token 密码
     * @param userCallBack 函数回调
     */

    public void logout(String token, final UserCallBack userCallBack) {
        // TODO Auto-generated method stub
        setUserService();
        userService.logout(token, userCallBack);
    }

    /**
     *  获取其他用户列表
     * @param token
     * @param userCallBack
     */
    public void getUserMList(String token, final UserCallBack userCallBack){
        setUserService();
        userService.getUserMList(token, userCallBack);
    }

    /**
     *  获取用户列表
     * @param token
     * @param userCallBack
     */
    public void getUserList(String token, final UserCallBack userCallBack){
        setUserService();
        userService.getUserList(token, userCallBack);
    }

    /**
     *  注册账号信息
     *  @param deviceBean 设备信息
     *  @param isStandBy 是否是备用机 0：设备机 1;备用机[必填]
     */
    public void registerAccount(DeviceBean deviceBean, boolean isStandBy, CompanyBean company, CompanyUserBean userBean, UserCallBack userCallBack) {
        userService = new UserServiceImpl(mContext);
        userService.registerAccount(deviceBean, isStandBy, company, userBean, userCallBack);
    }

    /**
     *  注册账号信息
     *  @param imei
     *  @param userCallBack
     */
    public void getRegisterDeviceInfo(String imei, UserCallBack userCallBack) {
        userService = new UserServiceImpl(mContext);
        userService.getRegisterDeviceInfo(imei, userCallBack);
    }

    /**
     *  获取公司列表(注册)
     * @param userCallBack
     */
    public void getCompanyList(final UserCallBack userCallBack){
        userService = new UserServiceImpl(mContext);
        userService.getCompanyList(userCallBack);
    }

    /**
     *  获取该公司绑定的用户列表(注册)
     *  @param companyid 公司id
     * @param userCallBack
     */
    public void getCompanyUserList(String companyid, final UserCallBack userCallBack){
        userService = new UserServiceImpl(mContext);
        userService.getCompanyUserList(companyid, userCallBack);
    }

    /**
     *  获取部门列表
     */
    public void getDepartmentList(Context context, int pageindex, int pagesize, boolean filtered, final UserCallBack userCallBack) {
        userService = new UserServiceImpl(mContext);
        userService.getDepartmentList(context, pageindex, pagesize, filtered, userCallBack);
    }

    /**
     *  获取该部门的用户列表
     */
    public void getDepartmentUserList(Context context, String departId, int pageindex, int pagesize, final UserCallBack userCallBack) {
        userService = new UserServiceImpl(mContext);
        userService.getDepartmentUserList(context, departId, pageindex, pagesize, userCallBack);
    }

    /**
     *  获取可用车辆列表
     */
    public void getCarList(String token, final UserCallBack userCallBack){
        setUserService();
        userService.getCarList(token, userCallBack);
    }

    /**
     * 绑定车辆信息
     * @param token
     * @param carid 车牌号码
     */
    public void userBindCar(String token, String carid, final UserCallBack userCallBack){
        setUserService();
        userService.userBindCar(token, carid, userCallBack);
    }

    /**
     * 解绑车辆信息
     */
    public void userUnBindCar(String token, String carid, final UserCallBack userCallBack){
        setUserService();
        userService.userUnBindCar(token, carid, userCallBack);
    }

    /**
     *  获取用户地图配置信息
     */
    public void getMapConfigData(String userid, final UserCallBack userCallBack){
        setUserService();
        userService.getMapConfigData(userid, userCallBack);
    }

    /**
     *  获取地图图层属性排序配置
     */
    public void getMapLayerIndexConfigData(String token, final UserCallBack userCallBack){
        setUserService();
        userService.getMapLayerIndexConfigData(token, userCallBack);
    }

    /**
     *  获取分公司地图图层列表
     */
    public void getMapLayersData(String token, final UserCallBack userCallBack){
        setUserService();
        userService.getMapLayersData(token, userCallBack);
    }

    /**
     * 设备绑定校验 返回结果 0[未绑定],1[已绑定],2[绑定错误]
     */
    public void checkPdaBinded(Context context, UserCallBack userCallBack) {
        userService = new UserServiceImpl(mContext);
        userService.checkPdaBinded(context, userCallBack);
    }

    /**
     * 设备绑定
     */
    public void bindPdaDevice(Context context, DeviceBean deviceBean, UserCallBack userCallBack) {
        userService = new UserServiceImpl(mContext);
        userService.bindPdaDevice(context, deviceBean, userCallBack);
    }

    /**
     *  获取用户gisToken
     */
    public void getUserGisToken(Context context, final UserCallBack userCallBack) {
        userService = new UserServiceImpl(mContext);
        userService.getUserGisToken(context, userCallBack);
    }
}
