package quant.rich.emoney.controller.manage;

import java.io.IOException;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.JsonNode;

import lombok.extern.slf4j.Slf4j;
import quant.rich.emoney.controller.common.BaseController;
import quant.rich.emoney.entity.sqlite.StrategyAndPool;
import quant.rich.emoney.exception.RException;
import quant.rich.emoney.pojo.dto.LayPageReq;
import quant.rich.emoney.pojo.dto.LayPageResp;
import quant.rich.emoney.pojo.dto.R;
import quant.rich.emoney.service.sqlite.StrategyAndPoolService;

@Slf4j
@Controller
@RequestMapping("/admin/v1/manage/strategyAndPool")
public class StrategyAndPoolControllerV1 extends BaseController {
    
    @Autowired
    StrategyAndPoolService strategyAndPoolService;
	
	@GetMapping({"", "/", "/index"})
	public String index() {
	    System.out.println(strategyAndPoolService.exportPreparedStrategies().toPrettyString());
		return "/admin/v1/manage/strategyAndPool/index";
	}
	
	@GetMapping("/list")
	@ResponseBody
	public LayPageResp<?> list(LayPageReq<StrategyAndPool> pageReq) {
	    Page<StrategyAndPool> planPage = strategyAndPoolService.page(pageReq,
	            new LambdaQueryWrapper<StrategyAndPool>()
	            .orderByAsc(StrategyAndPool::getStrategyId, StrategyAndPool::getPoolId));
	    return new LayPageResp<>(planPage);
	}

    @GetMapping("/getOne")
    @ResponseBody
    public R<?> getOne(Integer poolId) {
        
        // 如果 planId 是空，说明可能希望新建一个 Plan，需要返回默认实例化对象
        if (poolId == null) {
            return R.ok(new StrategyAndPool());
        }
        
        // 否则从数据库取
        StrategyAndPool strategyAndPool = strategyAndPoolService.getById(poolId);
        return R.judge(strategyAndPool != null, strategyAndPool, "无法找到对应 ID 的 StrategyAndPool");
    }

    @PostMapping("/save")
    @ResponseBody
    public R<?> save(@RequestBody StrategyAndPool strategyAndPool) {
        if (Objects.nonNull(strategyAndPool.getPoolId())) {
            return R.judge(
                    strategyAndPoolService.saveOrUpdate(strategyAndPool), "保存失败");
        }
        throw RException.badRequest("poolId 不允许为空");
    }
    
    @PostMapping("/delete")
    @ResponseBody
    public R<?> delete(Integer poolId) {
        return R.judge(strategyAndPoolService.removeById(poolId), "删除失败，是否已删除？");
    }

    /**
     * 导出 prepared_strategies.json
     * @throws IOException
     */
    @GetMapping("/exportPreparedStrategies")
    @ResponseBody
    public void exportPreparedStrategies() throws IOException {
        
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"prepared_strategies.json\"");
        
        JsonNode json = strategyAndPoolService.exportPreparedStrategies();
        response.getWriter().write(json.toPrettyString());
    }
}
