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

import android.content.Context;

import com.tofly.ghrq_route.base.URLResponseCode.ErrorCode;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
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.DepartListBean;
import com.tofly.ghrq_route.module.user.domain.DepartUserBean;
import com.tofly.ghrq_route.module.user.domain.DepartUserListBean;
import com.tofly.ghrq_route.module.user.domain.DeviceBean;
import com.tofly.ghrq_route.module.user.domain.MessageUserBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.module.user.domain.UserMapDataBean;
import com.tofly.ghrq_route.module.user.domain.UserOnlineMapDataBean;
import com.tofly.ghrq_route.common.util.sharedPreference.SharePreUtil;

import java.util.ArrayList;
import java.util.List;

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

public class UserServiceImplOffline implements IUserService {
    private Context mContext;

    private DBDaoHelper dbDaoHelper;

    public UserServiceImplOffline(Context context) {
        mContext = context;
        dbDaoHelper = DBDaoHelper.getInstance();
    }

    @Override
    public void login(final String userName, final String token, final boolean forceLogin, final UserCallBack userCallBack) {
        /*判断用户是否登陆过，并且拿到数据库中的用户token，否则则视为未登录过*/
        UserBean rememberedUser = SharePreUtil.getLoginInfo(mContext);
        boolean isStandBy = SharePreUtil.getDeviceIsStandBy(mContext);
        boolean isNameEqual = false;
        boolean isPasswordEqual = false;
        boolean isUserIdEqual = false;
        String save_token = "";
        UserBean save_user = null;
        if (rememberedUser != null) {
            String saveName = rememberedUser.getUsername();
            if (saveName != null && !saveName.isEmpty()) {
                isNameEqual = userName.equals(saveName);
            }

            if (isNameEqual) {
                String savePassword = rememberedUser.getPassword();
                if (savePassword != null && !savePassword.isEmpty()) {
                    isPasswordEqual = token.equals(savePassword);
                }

                String user_id = rememberedUser.getUserid();
                if (user_id != null && !user_id.isEmpty()) {
                    UserBean savedUser = dbDaoHelper.queryUserByUserId(user_id);
                    if (savedUser != null) {
                        isUserIdEqual = savedUser.getUserid().equals(user_id);
                        if (isUserIdEqual) {
                            save_token = savedUser.getToken();
                            save_user = savedUser;
                        }
                    }
                }
            } else {
                if (isStandBy) {
                    UserBean dbUser = dbDaoHelper.queryUserByLoginid(userName);
                    if (dbUser != null) {
                        String savePassword = dbUser.getPassword();
                        if (savePassword != null && !savePassword.isEmpty()) {
                            //String save_password = BaseApp.AESDecodeString(savePassword);
                            isPasswordEqual = token.equals(savePassword);

                            if (isPasswordEqual) {
                                save_token = dbUser.getToken();
                                save_user = dbUser;
                            }
                        }
                    }
                }
            }
        }

        boolean isSuccess = false;
        int err_code = ErrorCode.PASSWORD_ERROR;
        if (isNameEqual) {
            if (isPasswordEqual && isUserIdEqual) {
                isSuccess = true;
                err_code = ErrorCode.SUCCESS_OFFLINE;
            }
        } else {
            err_code = ErrorCode.LOGIN_RESIST;
            if (isStandBy && isPasswordEqual) {
                isSuccess = true;
                err_code = ErrorCode.SUCCESS_OFFLINE;
                dbDaoHelper.clearAllButUser();
                SharePreUtil.setSignSectionToday(0);
            }
        }
        userCallBack.onLogin(isSuccess, err_code, save_token, save_user);
    }

    @Override
    public void getAccountInfo(final String username, final String token, final UserCallBack userCallBack) {
        UserBean userBean = dbDaoHelper.queryUserByLoginid(username);
        int err_code = ErrorCode.SUCCESS_OFFLINE;
        userCallBack.onGetAccountInfo(true, err_code, ErrorCode.getErrorMessage(err_code), userBean);
    }

    @Override
    public void getUserMenus(Context context, UserCallBack userCallBack) {

    }

    @Override
    public void updateAccountInfo(String userid, String username, String mobile, String password, String avatar, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onUpdateAccount(false, err_code, ErrorCode.getErrorMessage(err_code));
    }

    @Override
    public void getUserInfo(final String userid, final String token, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetUserInfo(false, err_code, ErrorCode.getErrorMessage(err_code), null, null);
    }

    @Override
    public void logout(final String token, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onLogout(false, err_code, ErrorCode.getErrorMessage(err_code));
    }

    @Override
    public void getUserMList(final String token, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetUserMList(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void getUserList(final String token, final UserCallBack userCallBack) {
        int err_code;
        List<DepartUserBean> data = new ArrayList<>();
        List<DepartListBean> departListBeans = dbDaoHelper.getDepartList();
        List<DepartUserListBean> departUserListBeans = dbDaoHelper.getDepartUserList();
        if (departListBeans != null && departListBeans.size() > 0) {
            err_code = 0;
            for (DepartListBean departListBean : departListBeans) {
                DepartUserBean depart = new DepartUserBean();
                depart.setId(Integer.parseInt(departListBean.getId()));
                depart.setName(departListBean.getName());
                List<MessageUserBean> userList = new ArrayList<>();
                for (DepartUserListBean departUserListBean : departUserListBeans) {
                    if (departUserListBean.getDepartmentId().equals(departListBean.getId())) {
                        MessageUserBean messageUserBean = new MessageUserBean();
                        messageUserBean.setId(Integer.parseInt(departUserListBean.getId()));
                        messageUserBean.setRealName(departUserListBean.getRealName());
                        userList.add(messageUserBean);
                    }
                }
                depart.setUser(userList);
                data.add(depart);
            }
        } else {
            err_code = ErrorCode.LOGIN_OFFLINE;
        }
        userCallBack.onGetUserList(true, err_code, ErrorCode.getErrorMessage(err_code), data);
    }

    @Override
    public void registerAccount(final DeviceBean deviceBean, final boolean isStandBy, final CompanyBean companyBean, final CompanyUserBean userBean, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onRegisterAccount(false, err_code, ErrorCode.getErrorMessage(err_code));
    }

    @Override
    public void getRegisterDeviceInfo(final String imei, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetRegisterDeviceInfo(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void getCompanyList(final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetCompanyList(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void getCompanyUserList(final String companyid, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetCompanyUserList(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void getDepartmentList(Context context, int pageindex, int pagesize, boolean filtered, UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetDepartmentList(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void getDepartmentUserList(Context context, String departId, int pageindex, int pagesize, UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetDepartmentUserList(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void getCarList(final String token, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetCarList(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void userBindCar(final String token, final String carid, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onUserBindCar(false, err_code, ErrorCode.getErrorMessage(err_code));
    }

    @Override
    public void userUnBindCar(final String token, final String carid, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onUserUnBindCar(false, err_code, ErrorCode.getErrorMessage(err_code));
    }

    @Override
    public void getMapConfigData(String userid, final UserCallBack userCallBack) {
        List<UserOnlineMapDataBean> urlConfigData = dbDaoHelper.getUserOnlineMapDataList(userid);
        List<UserMapDataBean> userMapDataList = dbDaoHelper.getUserMapDataList(userid);
        userCallBack.onGetMapConfigData(true, ErrorCode.SUCCESS_OFFLINE, "", urlConfigData, userMapDataList);
    }

    @Override
    public void getMapLayerIndexConfigData(final String token, final UserCallBack userCallBack) {
        int err_code = ErrorCode.LOGIN_OFFLINE;
        userCallBack.onGetMapLayerIndexConfigData(false, err_code, ErrorCode.getErrorMessage(err_code), null);
    }

    @Override
    public void getMapLayersData(String token, UserCallBack userCallBack) {

    }

    @Override
    public void checkPdaBinded(Context context, UserCallBack userCallBack) {

    }

    @Override
    public void bindPdaDevice(Context context, DeviceBean deviceBean, UserCallBack userCallBack) {

    }

    @Override
    public void getUserGisToken(Context context, UserCallBack userCallBack) {

    }
}
