package quant.rich.emoney.controller.manage;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import quant.rich.emoney.controller.common.BaseController;
import quant.rich.emoney.entity.config.IndexInfoConfig;
import quant.rich.emoney.pojo.dto.LayPageResp;
import quant.rich.emoney.pojo.dto.R;

@Controller
@RequestMapping("/admin/v1/manage/indexInfo")
public class IndexInfoControllerV1 extends BaseController {
    
    @Autowired
    IndexInfoConfig indexInfo;

    @GetMapping({"", "/", "/index"})
    public String index() {
        return "/admin/v1/manage/indexInfo/index";
    }

    @GetMapping("/configIndOnline")
    @ResponseBody
    public R<?> configIndOnline(String url) throws IOException {
        
        //return R.judge(() -> indexInfo.getOnlineConfigByUrl(url));
        return R.ok(indexInfo.getConfigIndOnline());
    }
    
    @GetMapping("/getFields")
    @ResponseBody
    public R<?> getFields(@RequestParam("fields") String[] fields) {
        if (fields == null || fields.length == 0) {
            return R.ok(indexInfo);
        }
        ObjectNode indexInfoJson = new ObjectMapper().valueToTree(indexInfo);
        Map<String, Object> map = new HashMap<>();
        for (String field : fields) {
            map.put(field, indexInfoJson.get(field));
        }
        return R.ok(map);
    }
    
    @GetMapping("/getIndexDetail")
    @ResponseBody
    public R<?> getIndexDetail(String code) {
        return null;
    }
    
    
    @GetMapping("/getConfigIndOnlineByUrl")
    @ResponseBody
    public R<?> getConfigOnlineByUrl(String url) {
        return R.judge(() -> indexInfo.getOnlineConfigByUrl(url));
    }
    
    @GetMapping("/getIndexInfoConfig")
    @ResponseBody
    public R<?> getIndexInfoConfig() {
        return R.ok(indexInfo);
    }
    
    @GetMapping("/list")
    @ResponseBody
    public LayPageResp<?> list() {
        return null;
    }
}
