package com.qxgmat.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.dao.entity.Rank;
import com.qxgmat.data.dao.entity.Setting;
import com.qxgmat.dto.admin.request.RankDto;
import com.qxgmat.service.inline.RankService;
import com.qxgmat.service.inline.SettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController("AdminSettingController")
@RequestMapping("/admin/setting")
@Api(tags = "配置信息接口", description = "全局独立配置设置", produces = MediaType.APPLICATION_JSON_VALUE)
public class SettingController {
    private static final Logger logger = LoggerFactory.getLogger(SettingController.class);

    @Autowired
    private SettingService settingService;

    @Autowired
    private RankService rankService;

    @RequestMapping(value = "/index", method = RequestMethod.PUT)
    @ApiOperation(value = "修改首页配置", httpMethod = "PUT")
    private Response<Boolean> editIndex(@RequestBody @Validated JSONObject dto){
        Setting entity = settingService.getByKey(SettingKey.INDEX);
        entity.setValue(dto);
        settingService.edit(entity);
        return ResponseHelp.success(true);
    }

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

    @RequestMapping(value = "/place", method = RequestMethod.PUT)
    @ApiOperation(value = "修改考点设置", httpMethod = "PUT")
    private Response<Boolean> editPlace(@RequestBody @Validated JSONObject dto){
        Setting entity = settingService.getByKey(SettingKey.PLACE);
        entity.setValue(dto);
        settingService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/place", method = RequestMethod.GET)
    @ApiOperation(value = "获取考点配置", httpMethod = "GET")
    private Response<JSONObject> getPlace(){
        Setting entity = settingService.getByKey(SettingKey.PLACE);

        return ResponseHelp.success(entity.getValue());
    }


    @RequestMapping(value = "/sentence", method = RequestMethod.PUT)
    @ApiOperation(value = "修改长难句设置", httpMethod = "PUT")
    private Response<Boolean> editSentence(@RequestBody @Validated JSONObject dto){
        Setting entity = settingService.getByKey(SettingKey.SENTENCE);
        entity.setValue(dto);
        settingService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/sentence", method = RequestMethod.GET)
    @ApiOperation(value = "获取长难句配置", httpMethod = "GET")
    private Response<JSONObject> getSentence(){
        Setting entity = settingService.getByKey(SettingKey.SENTENCE);

        return ResponseHelp.success(entity.getValue());
    }


    @RequestMapping(value = "/exercise_time", method = RequestMethod.PUT)
    @ApiOperation(value = "修改做题时间设置", httpMethod = "PUT")
    private Response<Boolean> editExerciseTime(@RequestBody @Validated JSONObject dto){
        Setting entity = settingService.getByKey(SettingKey.EXERCISE_TIME);
        entity.setValue(dto);
        settingService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/exercise_time", method = RequestMethod.GET)
    @ApiOperation(value = "获取做题时间配置", httpMethod = "GET")
    private Response<JSONObject> getExerciseTime(){
        Setting entity = settingService.getByKey(SettingKey.EXERCISE_TIME);

        return ResponseHelp.success(entity.getValue());
    }
    @RequestMapping(value = "/examination_time", method = RequestMethod.PUT)
    @ApiOperation(value = "修改做题时间设置", httpMethod = "PUT")
    private Response<Boolean> editExaminationTime(@RequestBody @Validated JSONObject dto){
        Setting entity = settingService.getByKey(SettingKey.EXAMINATION_TIME);
        entity.setValue(dto);
        settingService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/examination_time", method = RequestMethod.GET)
    @ApiOperation(value = "获取做题时间配置", httpMethod = "GET")
    private Response<JSONObject> getExaminationTime(){
        Setting entity = settingService.getByKey(SettingKey.EXAMINATION_TIME);

        return ResponseHelp.success(entity.getValue());
    }

    @RequestMapping(value = "/filter_time", method = RequestMethod.PUT)
    @ApiOperation(value = "修改剔除时间设置", httpMethod = "PUT")
    private Response<Boolean> editFilterTime(@RequestBody @Validated JSONObject dto){
        Setting entity = settingService.getByKey(SettingKey.FILTER_TIME);
        entity.setValue(dto);
        settingService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/filter_time", method = RequestMethod.GET)
    @ApiOperation(value = "获取做题时间配置", httpMethod = "GET")
    private Response<JSONObject> getFilterTime(){
        Setting entity = settingService.getByKey(SettingKey.FILTER_TIME);

        return ResponseHelp.success(entity.getValue());
    }

    @RequestMapping(value = "/tips", method = RequestMethod.PUT)
    @ApiOperation(value = "修改结构说明", httpMethod = "PUT")
    private Response<Boolean> editTips(@RequestBody @Validated JSONObject dto){
        Setting entity = settingService.getByKey(SettingKey.TIPS);
        entity.setValue(dto);
        settingService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/tips", method = RequestMethod.GET)
    @ApiOperation(value = "获取结构说明", httpMethod = "GET")
    private Response<JSONObject> getTips(){
        Setting entity = settingService.getByKey(SettingKey.TIPS);

        return ResponseHelp.success(entity.getValue());
    }

    @RequestMapping(value = "/rank/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加排行", httpMethod = "POST")
    private Response<Boolean> addRank(@RequestBody @Validated RankDto dto){
        Rank entity = Transform.dtoToEntity(dto);
        rankService.add(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/rank/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改排行", httpMethod = "PUT")
    private Response<Boolean> editRank(@RequestBody @Validated RankDto dto){
        Rank entity = Transform.dtoToEntity(dto);
        rankService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/rank/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除排行", httpMethod = "DELETE")
    private Response<Boolean> deleteRank(@RequestParam int id){
        rankService.delete(id);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/rank/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取排行设置", httpMethod = "GET")
    private Response<List<Rank>> getRank(){
        List<Rank> rankList = rankService.all();
        return ResponseHelp.success(rankList);
    }
}
