package com.bizzan.bitrade.controller.system;

import com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.constant.CommonStatus;
import com.bizzan.bitrade.constant.PageModel;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.dao.CoinNetworkDao;
import com.bizzan.bitrade.entity.CoinNetwork;
import com.bizzan.bitrade.service.CoinNetworkService;
import com.bizzan.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 主网配置管理
 */
@RestController
@RequestMapping("/system/coin-network")
@Slf4j
public class CoinNetworkController extends BaseAdminController {

    @Autowired
    private CoinNetworkService coinNetworkService;

    @Autowired
    private CoinNetworkDao coinNetworkDao;

    @RequiresPermissions("system:coin-network:page-query")
    @PostMapping("page-query")
    @AccessLog(module = AdminModule.SYSTEM, operation = "分页查询主网配置")
    public MessageResult pageQuery(PageModel pageModel) {
        PageRequest pageable = new PageRequest(
                pageModel.getPageNo() - 1,
                pageModel.getPageSize(),
                new Sort(Sort.Direction.ASC, "sort")
        );
        Page<CoinNetwork> pageResult = coinNetworkDao.findAll(pageable);
        return success(pageResult);
    }

    @RequiresPermissions("system:coin-network:detail")
    @PostMapping("detail")
    @AccessLog(module = AdminModule.SYSTEM, operation = "查询主网详情")
    public MessageResult detail(@RequestParam("id") Long id) {
        CoinNetwork network = coinNetworkService.findById(id);
        if (network == null) {
            return error("主网不存在");
        }
        return success(network);
    }

    @RequiresPermissions("system:coin-network:create")
    @PostMapping("create")
    @AccessLog(module = AdminModule.SYSTEM, operation = "新增主网配置")
    public MessageResult create(@Valid CoinNetwork coinNetwork, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return error(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            coinNetworkService.save(coinNetwork);
        } catch (IllegalArgumentException e) {
            return error(e.getMessage());
        }
        return success();
    }

    @RequiresPermissions("system:coin-network:update")
    @PostMapping("update")
    @AccessLog(module = AdminModule.SYSTEM, operation = "更新主网配置")
    public MessageResult update(@Valid CoinNetwork coinNetwork, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return error(bindingResult.getFieldError().getDefaultMessage());
        }
        if (coinNetwork.getId() == null) {
            return error("主网ID不能为空");
        }
        coinNetworkService.update(coinNetwork);
        return success();
    }

    @RequiresPermissions("system:coin-network:delete")
    @PostMapping("delete")
    @AccessLog(module = AdminModule.SYSTEM, operation = "删除主网配置")
    public MessageResult delete(@RequestParam("id") Long id) {
        coinNetworkService.delete(id);
        return success();
    }

    @RequiresPermissions("system:coin-network:update-status")
    @PostMapping("update-status")
    @AccessLog(module = AdminModule.SYSTEM, operation = "更新主网状态")
    public MessageResult updateStatus(@RequestParam("id") Long id, @RequestParam("status") CommonStatus status) {
        coinNetworkService.updateStatus(id, status);
        return success();
    }

    /**
     * 获取所有主网列表（下拉选择用）
     */
    @PostMapping("all")
    @AccessLog(module = AdminModule.SYSTEM, operation = "获取所有主网列表")
    public MessageResult all() {
        return success(coinNetworkService.findAll());
    }
}
