package com.bizzan.bitrade.controller.system;

import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.ConfigCoinSymbol;
import com.bizzan.bitrade.service.ConfigCoinSymbolService;
import com.bizzan.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/config-coin-symbol")
@Slf4j
public class ConfigCoinSymbolController extends BaseAdminController {

    @Autowired
    private ConfigCoinSymbolService configCoinSymbolService;

    @PostMapping("page-query")
    public MessageResult pageQuery(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize,
            @RequestParam(required = false) String coinSymbol,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime
    ) {
        Pageable pageable = new PageRequest(pageNo - 1, pageSize, new Sort(Sort.Direction.DESC, "mtime"));
        Page<ConfigCoinSymbol> page = configCoinSymbolService.pageQuery(coinSymbol, startTime, endTime, pageable);
        return success(page);
    }

    @GetMapping("list")
    public MessageResult list() {
        return success(configCoinSymbolService.findAll());
    }

    @GetMapping("detail")
    public MessageResult detail(@RequestParam Long id) {
        ConfigCoinSymbol coin = configCoinSymbolService.findOne(id);
        Assert.notNull(coin, "记录不存在");
        return success(coin);
    }

    @PostMapping("create")
    public MessageResult create(@RequestBody ConfigCoinSymbol coin) {
        if (configCoinSymbolService.findByCoinSymbol(coin.getCoinSymbol()) != null) {
            return error("币种已存在: " + coin.getCoinSymbol());
        }
        return success(configCoinSymbolService.create(coin));
    }

    @PostMapping("update")
    public MessageResult update(@RequestBody ConfigCoinSymbol coin) {
        ConfigCoinSymbol existing = configCoinSymbolService.findOne(coin.getId());
        Assert.notNull(existing, "记录不存在");
        ConfigCoinSymbol dup = configCoinSymbolService.findByCoinSymbol(coin.getCoinSymbol());
        if (dup != null && !dup.getId().equals(coin.getId())) {
            return error("币种已存在: " + coin.getCoinSymbol());
        }
        existing.setCoinSymbol(coin.getCoinSymbol());
        existing.setShowPrecision(coin.getShowPrecision());
        existing.setIsQuote(coin.getIsQuote());
        existing.setIsOpen(coin.getIsOpen());
        if (coin.getIcon() != null) existing.setIcon(coin.getIcon());
        existing.setSort(coin.getSort());
        existing.setName(coin.getName());
        if (coin.getContractPrecision() != null) existing.setContractPrecision(coin.getContractPrecision());
        if (coin.getTokenBase() != null) existing.setTokenBase(coin.getTokenBase());
        if (coin.getTagType() != null) existing.setTagType(coin.getTagType());
        return success(configCoinSymbolService.update(existing));
    }

    @PostMapping("delete")
    public MessageResult delete(@RequestParam Long id) {
        configCoinSymbolService.delete(id);
        return success();
    }

    @PostMapping("toggle-status")
    public MessageResult toggleStatus(@RequestParam Long id) {
        ConfigCoinSymbol coin = configCoinSymbolService.findOne(id);
        Assert.notNull(coin, "记录不存在");
        configCoinSymbolService.toggleStatus(id);
        return success();
    }
}
