package com.m2pool.pool.controller;

import com.github.pagehelper.PageHelper;
import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.common.core.web.Result.AjaxResult;
import com.m2pool.common.core.web.controller.BaseController;
import com.m2pool.common.core.web.page.TableDataInfo;
import com.m2pool.common.security.annotation.RequiresLogin;
import com.m2pool.pool.dto.UserApiDto;
import com.m2pool.pool.service.GoogleAuthService;
import com.m2pool.pool.service.MinerAccountService;
import com.m2pool.pool.service.UserMinserService;
import com.m2pool.pool.vo.*;
import com.m2pool.system.api.RemoteUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description 统一请求接口
 * @Date 2024/6/14 9:54
 * @Author dy
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserMinserService umService;

    @Autowired
    private GoogleAuthService googleAuthService;

    @Autowired
    private MinerAccountService maService;

    @Autowired
    private RemoteUserService userService;

    @PostMapping("/addMinerAccount")
    @ApiOperation(value = "绑定挖矿账号")
    @RequiresLogin
    public AjaxResult addMinerAccount(@Valid @RequestBody MinerAccountAddVo vo){

        return  umService.addMinerAccount(vo);

    }

    @PostMapping("/checkAccount")
    @ApiOperation(value = "校验挖矿账号是否存在")
    @RequiresLogin
    public AjaxResult checkAccount(@Valid @RequestBody MinerAccountAddVo vo){

        return  umService.checkAccount(vo);

    }

    @PostMapping("/checkBalance")
    @ApiOperation(value = "校验钱包可用性")
    @RequiresLogin
    public AjaxResult checkBalance(@Valid @RequestBody BalanceCheckVo vo){

        return  umService.checkBalance(vo);

    }

    @PostMapping("/check")
    @ApiOperation(value = "校验账户是否可用和钱包可用性")
    @RequiresLogin
    public AjaxResult check(@Valid @RequestBody MaBalanceCheckVo vo){

        return  umService.checkMaAndBalance(vo);

    }

    @DeleteMapping("/delMinerAccount")
    @ApiOperation(value = "删除挖矿账号")
    @RequiresLogin
    public AjaxResult delMinerAccount(@Valid @RequestBody MinerAccountDelVo vo){

        return  umService.delMinerAccount(vo);

    }

    @PostMapping("/addBalance")
    @ApiOperation(value = "挖矿账户绑定钱包-->>现改为 修改钱包地址或起付线以及切换自动提现激活状态")
    @RequiresLogin
    public AjaxResult addBalance(@Valid @RequestBody AccountBalanceAddVo vo){

        return  umService.addBalance(vo);

    }

    //@PostMapping("/updateBalance")
    //@ApiOperation(value = "挖矿账户解绑钱包")
    //@RequiresLogin
    //public AjaxResult updateBalance(@Valid @RequestBody AccountBalanceAddVo com.m2pool.chat.vo){
    //
    //    return  umService.updateBalance(com.m2pool.chat.vo);
    //
    //}

    @PostMapping("/getAccountList")
    @ApiOperation(value = "获取用户当前币种下所有挖矿账号")
    @RequiresLogin
    public AjaxResult getAccountList(){

        return  umService.getAccountList();

    }

    @PostMapping("/getAccountGradeList")
    @ApiOperation(value = "获取用户当前币种下所有挖矿账号")
    @RequiresLogin
    public AjaxResult getAccountGradeList(){

        return  umService.getAccountGradeList();

    }

    @PostMapping("/getMinerAccountBalance")
    @ApiOperation(value = "获取当前矿工账号绑定钱包地址")
    @RequiresLogin
    public AjaxResult getMinerAccountBalance(@RequestBody MinerIdVo vo){

        return  umService.getMinerAccountBalance(vo);

    }

    /****************************************  google两步验证相关接口  ***********************************************************/
    @PostMapping("/ifBind")
    @ApiOperation(value = "校验钱包可用性")
    @RequiresLogin
    public AjaxResult ifBind(){
        clearPage();
        return  googleAuthService.ifBind();

    }

    @PostMapping("/getBindInfo")
    @ApiOperation(value = "获取谷歌验证器 安全码和验证码")
    @RequiresLogin
    public AjaxResult getBindInfo(){
        clearPage();
        return  googleAuthService.getBindInfo();

    }

    @PostMapping("/getBindCode")
    @ApiOperation(value = "绑定谷歌验证器 发送邮箱验证码")
    @RequiresLogin
    public AjaxResult getBindCode(){
        clearPage();
        return  googleAuthService.getBindCode();

    }

    @PostMapping("/bindGoogle")
    @ApiOperation(value = "绑定谷歌验证器")
    @RequiresLogin
    public AjaxResult bindGoogle(@Valid @RequestBody GoogleBindVo vo){
        clearPage();
        return  googleAuthService.bindGoogle(vo);

    }

    @PostMapping("/closeStepTwo")
    @ApiOperation(value = "关闭两步验证")
    @RequiresLogin
    public AjaxResult closeStepTwo(@Valid @RequestBody GoogleCloseVo vo){
        clearPage();
        return  googleAuthService.closeStepTwp(vo);

    }

    @PostMapping("/getCloseCode")
    @ApiOperation(value = "关闭谷歌验证器 发送邮箱验证码")
    @RequiresLogin
    public AjaxResult getCloseCode(){
        clearPage();
        return  googleAuthService.getCloseCode();

    }

    @PostMapping("/emailIfBind")
    @ApiOperation(value = "校验钱包可用性")
    public AjaxResult emailIfBind(@Valid @RequestBody EmailVo vo){
        clearPage();
        return  googleAuthService.emailIfBind(vo);

    }

    @PostMapping("/getApiKey")
    @ApiOperation(value = "创建apiKey")
    @RequiresLogin
    public AjaxResult getApiKey(@Valid @RequestBody ApiKeyCreateVo vo){

        return  umService.createApiKey(vo);

    }

    @PostMapping("/getApiList")
    @ApiOperation(value = "获取当前用户apiKEY列表 ")
    @RequiresLogin
    public TableDataInfo getApiList(@RequestBody(required = false) PageVo vo){

        if(StringUtils.isNull(vo)){
            vo = new PageVo();
        }

        PageHelper.startPage(vo.getPage(),vo.getLimit());
        List<UserApiDto> list = umService.getApiList(vo);
        return  getDataTable(list);

    }

    @PostMapping("/getApiInfo")
    @ApiOperation(value = "获取当前只读页面基础信息 ")
    @RequiresLogin
    public AjaxResult getApiInfo(@Valid @RequestBody ApiKeyInfoVo vo){
        return  umService.getApiInfo(vo);

    }


    @PostMapping("/updateAPI")
    @ApiOperation(value = "修改API绑定信息 只能修改备注和权限")
    @RequiresLogin
    public AjaxResult updateAPI(@Valid @RequestBody ApiKeyUpdateVo vo){

        return  umService.updateApi(vo);

    }

    @DeleteMapping("/delApi")
    @ApiOperation(value = "删除指定api 只能修改备注和权限")
    @RequiresLogin
    public AjaxResult delApi(@Valid @RequestBody ApiKeyDelVo vo){

        return  umService.deleteApi(vo);

    }
}
