package com.ho.business.controller;


import com.alibaba.fastjson.JSONObject;
import com.ho.business.entity.TabSort;
import com.ho.business.entity.TabTag;
import com.ho.business.service.TabSortWebService;
import com.ho.business.vo.req.TabReqVo;
import com.ho.business.vo.req.TabSortReqVo;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(ContextConstant.BUSINESS + "tabSortWeb")
@Api(tags = "策略模块配置")
@Slf4j
public class TabSortWebController {

    @Autowired
    private TabSortWebService tabSortWebService;


    @PostMapping("/addTabSort")
    @ApiOperation(value = "新增页签内容")
    public DataResult addTabSort(@RequestBody @Valid TabSortReqVo tabSortReqVo) {
        tabSortWebService.addTabSort(tabSortReqVo);
        return DataResult.success();
    }


    @PostMapping("/editTabSort")
    @ApiOperation(value = "编辑页签内容")
    public DataResult editTabSort(@RequestBody @Valid TabSortReqVo tabSortReqVo) {
        tabSortWebService.updateByTabId(tabSortReqVo);
        return DataResult.success();
    }


    @PostMapping("/deleteTabSort")
    @ApiOperation(value = "删除页签内容")
    public DataResult deleteTabSort(@RequestParam("tabId") Integer tabId) {
        // 主要是删除整个页签
        tabSortWebService.deleteTabSort(tabId);
        return DataResult.success();
    }


    @PostMapping("/queryTabSortList")
    @ApiOperation(value = "查询页签列表")
    public DataResult<List<TabSort>> queryTabSortList(@RequestParam("stationId") Integer stationId) {
        DataResult<List<TabSort>> result = DataResult.success();
        List<TabSort> tabSorts = tabSortWebService.queryTabSortList(stationId);
        result.setData(tabSorts);
        return result;
    }


    @PostMapping("/queryTabTagList")
    @ApiOperation(value = "查询标签列表")
    public DataResult<List<TabTag>> queryTabTags(@RequestParam("tabId") Integer tabId,@RequestParam("stationId") Integer stationId) {
        DataResult<List<TabTag>> result = DataResult.success();
        List<TabTag> tabTagList = tabSortWebService.queryTabTags(tabId,stationId);
        result.setData(tabTagList);
        return result;
    }

    @PostMapping("/operateTabTag")
    @ApiOperation(value = "操作标签")
    public DataResult operateTabTag(@RequestBody TabSort tabSort) {
        tabSortWebService.operateTabTag(tabSort);
        return DataResult.success();
    }

    @PostMapping("/oneKeyCopy")
    @ApiOperation(value = "一键复制")
    public DataResult oneKeyCopy(@RequestParam("stationId") Integer stationId, @RequestParam("copyStationId") Integer copyStationId) {
        tabSortWebService.oneKeyCopy(stationId,copyStationId);
        return DataResult.success();
    }

    @PostMapping("/queryVersion")
    @ApiOperation(value = "查看版本号列表")
    public DataResult<List<Map<String,Object>>> queryVersion(@Param("stationId") Integer stationId) {
        DataResult<List<Map<String,Object>>> result = DataResult.success();
        List<Map<String,Object>> list=tabSortWebService.queryVersion(stationId);
        result.setData(list);
        return result;
    }

    @PostMapping("/showVersion")
    @ApiOperation(value = "版本预览")
    public DataResult<List<TabSort>> showVersion(@RequestBody TabReqVo tabReqVo) {
        DataResult<List<TabSort>> result = DataResult.success();
        List<TabSort> list=tabSortWebService.showVersion(tabReqVo);
        result.setData(list);
        return result;
    }

    @PostMapping("/toggleVersion")
    @ApiOperation(value = "切换版本")
    public DataResult toggleVersion(@RequestBody TabReqVo tabReqVo) {
        tabSortWebService.toggleVersion(tabReqVo);
        return DataResult.success();
    }

    @PostMapping("/getStationStatus")
    @ApiOperation(value = "获取电站状态")
    public DataResult<Integer> getStationStatus(@RequestParam("stationId") Integer stationId) {
        JSONObject stationStatus = tabSortWebService.getStationStatus(stationId);
        return DataResult.success(stationStatus);
    }

    @PostMapping("/chooseState")
    @ApiOperation(value = "选择投退状态")
    public DataResult chooseState(@RequestParam("tagId") String tagId,@RequestParam("tabId") Integer tabId){
        if( tabId == null ){
            throw new BusinessException(BaseResponseCode.NON_DATA);
        }
        int i = tabSortWebService.queryTabTagIsChangeover(tabId);
        if( i == 0 && tagId.equals("null") ){
            throw new BusinessException(BaseResponseCode.CHANGEOVER_IS_NON);
        }
        if( tagId != null && !tagId.equals("null")){
            tabSortWebService.chooseState(Integer.parseInt(tagId),tabId);
        } else {
            tabSortWebService.chooseState(null,tabId);
        }
        return DataResult.success();
    }

}
