package com.example.demo.module.login;

import com.example.demo.common.GlobalConfig;
import com.example.demo.common.RequestResult;
import com.example.demo.module.notice.ExternalNoticeController;
import com.example.demo.table.UserLogin;
import com.example.demo.table.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.jc.util.JCUtil;

/**
 * 服务-游戏登陆
 */
@Service
public class GameLoginService {
    public static GameLoginService ins;
    public GameLoginService() {
        ins = this;
    }

    @Autowired
    GameLoginRepository repository;

    public RequestResult login(String username, String password, int appServerIndex) {
        RequestResult requestResult = new RequestResult();
        UserLogin userLogin = repository.getUserLogin(username);
        if (userLogin == null) {
            requestResult.setCode(-1);
            requestResult.setMsg("账号未注册");
//        } else if (userLogin.getServerIndex() != appServerIndex) {
//            requestResult.setCode(-3);
//            requestResult.setMsg("该账号不属于当前服务区");
        } else if (userLogin.getPassword().equals(password)) {
            long timeout = System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7;
            userLogin.setToken_timeout(timeout);
            LoginToken loginToken = LoginToken.parseUserLogin(userLogin, appServerIndex);
            userLogin.setToken(loginToken.getLoginTokenForDatabase());
            repository.updateUserLogin(userLogin);
            requestResult.setData(loginToken.toClientToken());
            requestResult.setMsg("登录成功");
        } else {
            requestResult.setCode(-2);
            requestResult.setMsg("密码错误");
        }
        return requestResult;
    }

    public RequestResult loginByPhone(String phone) {
        Long phoneLong = Long.valueOf(phone);
        RequestResult requestResult = new RequestResult();
        if (phoneLong == null) {
            return null;
        }
        String username = "p-" + phone;
        String password = "phone";
        UserLogin userLogin = repository.getUserLogin(username);
        if (userLogin == null) {
            userLogin = new UserLogin();
            userLogin.setUsername(username);
            userLogin.setPassword(password);
            UserInfo userInfo = new UserInfo();
            userInfo.setPhone(phone);
            repository.insertNewUser(userLogin, userInfo);
            if (userLogin.getId() == 0) {
                userLogin = repository.getUserLogin(username);
            }
        }
        String token = JCUtil.uuid();
        long timeout = System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7;
        userLogin.setToken(token);
        userLogin.setToken_timeout(timeout);
        repository.updateUserLogin(userLogin);
        requestResult.setData(userLogin.getId() + "&" + token);
        requestResult.setMsg("登录成功");
        return requestResult;
    }

    public RequestResult loginByToken(String clientToken, String sceneName, int appServerIndex) {
        RequestResult requestResult = new RequestResult();
        LoginToken loginToken = LoginToken.parseClientToken(clientToken);
        boolean isSceneRight = "Home".equals(sceneName) || "HeartRateBand".equals(sceneName);
        if (loginToken != null && loginToken.isMD5SignRight()
                && loginToken.serverIndex == appServerIndex && isSceneRight) {
            UserLogin userLogin = repository.getUserLogin(loginToken.uid);
            if (userLogin != null
//                && userLogin.getServerIndex() == appServerIndex
                && userLogin.getToken().equals(loginToken.getLoginTokenForDatabase())
                && System.currentTimeMillis() < userLogin.getToken_timeout()
            ) {
                requestResult.setData(LoginToken.parseUserLogin(userLogin, appServerIndex).toClientToken());
                return requestResult;
            }
        }
        requestResult.setFailCode();
        return requestResult;
    }

    public RequestResult register(String username, String password, String email, String phone, int appServerIndex) {
        RequestResult requestResult = new RequestResult();
        if (!GlobalConfig.checkServerIndexValid(appServerIndex)) {
            requestResult.setMsg("服务区ID非法");
            requestResult.setFailCode();
        } else if (repository.getUserLogin(username) != null) {
            requestResult.setMsg("该账号已被注册");
            requestResult.setFailCode();
        } else {
            UserLogin userLogin = new UserLogin();
            userLogin.setUsername(username);
            userLogin.setPassword(password);
            userLogin.setServerIndex(appServerIndex);
            boolean res = repository.insertNewUser(userLogin, email, phone);
            if (res) {
                requestResult.setMsg("注册成功");
            } else {
                requestResult.setMsg("注册失败");
                requestResult.setFailCode();
            }
        }
        return requestResult;
    }

    public RequestResult completeUserInfo(
            String username, String password, String nickname, int gender, String birthday,
            String countryCode, String stateCode, String cityCode) {
        RequestResult requestResult = new RequestResult();
        UserLogin userLogin = repository.getUserLogin(username);
        if (userLogin == null) {
            requestResult.setCode(-1);
            requestResult.setMsg("用户不存在");
        } else if (!userLogin.getPassword().equals(password)) {
            requestResult.setCode(-2);
            requestResult.setMsg("验证密码不对");
        } else if (nickname.length() > 12) {
            requestResult.setCode(-3);
            requestResult.setMsg("昵称错误");
        } else if (gender != 1 && gender != 2) {
            requestResult.setCode(-4);
            requestResult.setMsg("性别错误");
        } else if (birthday.length() != 10) {
            requestResult.setCode(-5);
            requestResult.setMsg("生日错误");
        } else if (countryCode == null || stateCode == null || cityCode == null) {
            requestResult.setCode(-6);
            requestResult.setMsg("所在地区错误");
        } else {
            UserInfo userInfo = repository.getUserInfo(userLogin.getId());
            userInfo.setNickname(nickname);
            userInfo.setGender(gender);
            userInfo.setBirthday(birthday);
            userInfo.setCountry(countryCode);
            userInfo.setState(stateCode);
            userInfo.setCity(cityCode);
            repository.updateUserInfo(userInfo);
            requestResult.setMsg("保存成功");
        }
        return requestResult;
    }

    public void setUserOfflineTime(int userID) {
        repository.setUserOfflineTime(userID);
    }
}
