package com.example.demo.module.smartbowsdk;

import com.example.demo.common.GlobalConfig;
import com.example.demo.common.GlobalRoot;
import com.example.demo.common.RequestResult;
import com.example.demo.module.smartbowsdk.dto.SBS_UserInfo;
import com.example.demo.module.smartbowsdk.config.GameChannel;
import com.example.demo.module.smartbowsdk.table.GameChannelLogin;
import com.example.demo.table.Mac9Axis;
import com.example.demo.table.UserLogin;
import com.example.demo.table.user.UserInfo;
import org.springframework.web.bind.annotation.*;
import pers.jc.sql.SQL;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

@CrossOrigin
@RestController
@RequestMapping("/SmartBowSDK")
public class SmartBowSDKController {

    @PostMapping("/login")
    public RequestResult login(String gameID, String channelID, String username, String password) {
        RequestResult requestResult = new RequestResult();
        if (gameID == null || channelID == null || username == null || password == null)
        {
            requestResult.setMsg("不要传值为Null的参数");
            requestResult.setCode(-1);
            return requestResult;
        }
        boolean gameChannelExist = false;
        for (GameChannel gc: GameChannel.configList) {
            if (gc.getGameID().equals(gameID) && gc.getChannelID().equals(channelID)) {
                gameChannelExist = true;
                break;
            }
        }
        if (!gameChannelExist) {
            requestResult.setMsg("游戏ID和渠道ID不匹配");
            requestResult.setCode(-4);
            return requestResult;
        }
        UserLogin userLogin = GlobalRoot.db.selectOne(UserLogin.class, new SQL(){{
            WHERE("username=" + PARAM(username));
        }});
        if (userLogin == null)
        {
            requestResult.setMsg("账号不存在");
            requestResult.setCode(-2);
            return requestResult;
        }
        if (!password.equals(userLogin.getPassword()))
        {
            requestResult.setMsg("密码错误");
            requestResult.setCode(-3);
            return requestResult;
        }
        requestResult.setData(new SBS_LoginToken(userLogin).getToken());
        requestResult.setMsg("登录成功");
        GameChannelLogin gameChannelLogin = new GameChannelLogin();
        gameChannelLogin.setGameID(gameID);
        gameChannelLogin.setChannelID(channelID);
        gameChannelLogin.setUserID(userLogin.getId());
        gameChannelLogin.setLoginTime(new Timestamp(System.currentTimeMillis()));
        GlobalRoot.db.replace(gameChannelLogin);
        return requestResult;
    }

    @PostMapping("/getUserInfo")
    public RequestResult getUserInfo(String token) {
        RequestResult requestResult = new RequestResult();
        SBS_LoginToken loginToken = new SBS_LoginToken(token);
        if (!loginToken.isValid())
        {
            requestResult.setCode(-1);
            requestResult.setMsg("Token失效");
            return requestResult;
        }
        UserInfo userInfo = GlobalRoot.db.selectOne(UserInfo.class, new SQL(){{
            WHERE("id=" + PARAM(loginToken.id));
        }});
        if (userInfo == null)
        {
            requestResult.setMsg("读取用户信息失败");
            requestResult.setCode(-2);
            return requestResult;
        }
        SBS_UserInfo sbs_userInfo = new SBS_UserInfo();
        sbs_userInfo.setNickname(userInfo.getNickname());
        sbs_userInfo.setGender(userInfo.getGender());

        int avatarID = userInfo.getAvatarID();
        String avatarUrl = "https://smartbow-1310052819.cos.ap-shanghai.myqcloud.com/avatar/";
        if (avatarID < 7) avatarUrl += "Player" + avatarID;
        else avatarUrl += avatarID - 7;
        avatarUrl += ".jpg";
        sbs_userInfo.setAvatarUrl(avatarUrl);

        requestResult.setData(sbs_userInfo);
        requestResult.setMsg("获取用户信息成功");
        return requestResult;
    }

    @PostMapping("/saveCalibrateRecord")
    public RequestResult saveCalibrateRecord(HttpServletRequest request) {
        RequestResult requestResult = new RequestResult();
        String mac = request.getParameter("mac");
        String record = request.getParameter("record");
        if (mac == null || record == null) {
            requestResult.setCode(-1);
            requestResult.setMsg("请求参数不能为null");
            return requestResult;
        }
        Mac9Axis model = new Mac9Axis();
        model.setMac(mac);
        model.setAttitude(record);
        int replaceCount = GlobalRoot.db.replace(model);
        if (replaceCount <= 0) {
            requestResult.setCode(-2);
            requestResult.setMsg("写入数据失败");
            return requestResult;
        }
        requestResult.setMsg("保存成功");
        return requestResult;
    }

    @PostMapping("/getCalibrateRecord")
    public RequestResult getCalibrateRecord(HttpServletRequest request) {
        RequestResult requestResult = new RequestResult();
        String mac = request.getParameter("mac");
        if (mac == null) {
            requestResult.setCode(-1);
            requestResult.setMsg("请求参数不能为null");
            return requestResult;
        }
        Mac9Axis model = GlobalRoot.db.selectOne(Mac9Axis.class, new SQL(){{
            WHERE("mac = " + PARAM(mac));
        }});
        if (model == null) {
            requestResult.setCode(-2);
            requestResult.setMsg("读取数据失败");
            return requestResult;
        }
        requestResult.setData(model.getAttitude());
        requestResult.setMsg("获取成功");
        return requestResult;
    }
}
