package com.qxgmat.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.dao.entity.*;
import com.qxgmat.service.inline.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/base")
@Api(tags = "基本接口", description = "全站基本数据接口", produces = MediaType.APPLICATION_JSON_VALUE)
public class BaseController {

    @Autowired
    private SettingService settingService;

    @Autowired
    private AdService adService;

    @Autowired
    private RankService rankService;

    @Autowired
    private ExerciseStructService exerciseStructService;

    @Autowired
    private ExaminationStructService examinationStructService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ApiOperation(value = "获取首页配置", httpMethod = "GET")
    private Response<JSONObject> index(){
        Setting entity = settingService.getByKey(SettingKey.INDEX);
        return ResponseHelp.success(entity.getValue());
    }

    @RequestMapping(value = "/ad", method = RequestMethod.GET)
    @ApiOperation(value = "获取广告", notes = "获取广告列表", httpMethod = "GET")
    public Response<List<Ad>> ad()  {
        List<Ad> adList = adService.all();
        return ResponseHelp.success(adList);
    }

    @RequestMapping(value = "/tips", method = RequestMethod.GET)
    @ApiOperation(value = "获取提示信息", notes = "获取提示信息", httpMethod = "GET")
    public Response<Map<String, String>> tips(@RequestParam(required = true) String[] position)  {
        Setting entity = settingService.getByKey(SettingKey.TIPS);
        JSONObject value = entity.getValue();

        Map<String, String> map = new HashMap<>();
        for (String p: position) {
            map.put(p, value.getString(p));
        }
        return ResponseHelp.success(map);
    }

    @RequestMapping(value = "/score", method = RequestMethod.GET)
    @ApiOperation(value = "考分计算", notes = "获取考分排行信息", httpMethod = "GET")
    public Response<Rank> score(
            @RequestParam(required = true) Number total,
            @RequestParam(required = true) Number quant
    )  {
        Rank rank = rankService.compute(total, quant);
        return ResponseHelp.success(rank);
    }

    @RequestMapping(value = "/exercise/struct", method = RequestMethod.GET)
    @ApiOperation(value = "所有练习层级", httpMethod = "GET")
    public Response<List<ExerciseStruct>> exerciseStruct(HttpSession session) {
        List<ExerciseStruct> p = exerciseStructService.all();
        return ResponseHelp.success(p);
    }

    @RequestMapping(value = "/examination/struct", method = RequestMethod.GET)
    @ApiOperation(value = "所有模考层级", httpMethod = "GET")
    public Response<List<ExaminationStruct>> examinationStruct(HttpSession session) {
        List<ExaminationStruct> p = examinationStructService.all();
        return ResponseHelp.success(p);
    }
}
