package com.neusoft.smart.pos.org.controller;

import com.neusoft.smart.pos.dto.org.request.TableWithDishesRequest;
import com.neusoft.smart.pos.dto.org.request.TablesRequest;
import com.neusoft.smart.pos.dto.org.response.TableWithDishesResponse;
import com.neusoft.smart.pos.dto.org.response.TablesResponse;
import com.neusoft.smart.pos.dto.org.response.TablesWeChatResponse;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.org.constants.TableErrorCode;
import com.neusoft.smart.pos.org.service.PrintService;
import com.neusoft.smart.pos.org.service.TableService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/tables")
public class TablesController {
    
    @Resource
    private TableService tableService;
    
    @Resource
    private PrintService printService;
    
    @Value("${smartPosPlatform.tables.wechat-redirect-url}")
    private String jumpUrl;
    
    @PostMapping
    public ResponseData insert(TablesRequest record) {
        tableService.insert(record);
        return new ResponseData();
    }
    
    
    @GetMapping("/{id}")
    public ResponseData<TablesResponse> selectById(@PathVariable("id") Integer id) {
        return new ResponseData<>(tableService.selectById(id));
    }
    
    @GetMapping("/shop/{shopId}")
    public ResponseData<List<TablesResponse>> selectByShopId(@PathVariable("shopId") Integer shopId) {
        final List<TablesResponse> tablesResponses = tableService.selectByShopId(shopId)
                .parallelStream()
                .peek(table -> {
                    if (table.getGroupId() == null) {
                        table.setGroupId(-1);
                    }
                }).collect(Collectors.toList());
        return new ResponseData<>(tablesResponses);
    }
    
    @GetMapping("/wechat/{shopId}/{time}")
    public ResponseData<TablesWeChatResponse> selectWeChatTableByShopId(@PathVariable("shopId") Integer shopId, @PathVariable("time") Long time) {
        final TablesWeChatResponse tablesResponses = tableService.selectWeChatOrder4Pos(shopId, time, new Date());
        return new ResponseData<>(tablesResponses);
    }
    
    @PutMapping
    public ResponseData updateById(TablesRequest record) {
        tableService.update(record);
        return new ResponseData();
    }
    
    @DeleteMapping("/{id}")
    public ResponseData deleteById(@PathVariable("id") Integer id) {
        tableService.delete(id);
        return new ResponseData();
    }
    
    @GetMapping("/jump/{id}")
    public void jump(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException {
        response.sendRedirect(jumpUrl + id);
    }
    
    @ApiOperation(value = "开台接口", notes = "开台接口")
    @PutMapping("/{id}/founding")
    public ResponseData<TableWithDishesResponse> founding(@PathVariable("id") Integer id) {
        ResponseData<TableWithDishesResponse> responseData = new ResponseData<>(tableService.founding(id));
        if (null != responseData.getResult()) {
            responseData.setCode(TableErrorCode.TABLE_OCCUPIED.getCode());
            responseData.setMessage(TableErrorCode.TABLE_OCCUPIED.getMessage());
        } else {
            TableWithDishesResponse tableWithDishesResponse = new TableWithDishesResponse();
            tableWithDishesResponse.setCreateTime(new Date());
            responseData.setResult(tableWithDishesResponse);
        }
        return responseData;
    }
    
    @ApiOperation(value = "开台后保存接口", notes = "开台后保存接口")
    @PostMapping("/{id}/founding")
    public ResponseData<?> saveFoundingData(@PathVariable("id") Integer id, @RequestBody TableWithDishesRequest tableWithDishesRequest) {
        tableService.saveFoundingData(id, tableWithDishesRequest);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "开台后添加新菜接口", notes = "开台后添加新菜接口")
    @PatchMapping("/{id}/founding")
    public ResponseData addDishes(@PathVariable("id") Integer id, @RequestBody TableWithDishesRequest tableWithDishesRequest) {
        tableService.addDishes(id, tableWithDishesRequest);
        return new ResponseData();
    }
    
    @ApiOperation(value = "结算后修改餐桌状态接口", notes = "结算后修改餐桌状态接口")
    @PutMapping("/{id}/settle")
    public ResponseData settle(@PathVariable("id") Integer id, @RequestBody TableWithDishesRequest tableWithDishesRequest) {
        tableService.settle(id, tableWithDishesRequest);
        return new ResponseData();
    }
    
    @ApiOperation(value = "清台接口", notes = "清台接口")
    @DeleteMapping("/{id}/founding")
    public ResponseData unfounding(@PathVariable("id") Integer id) {
        tableService.unfounding(id);
        return new ResponseData();
    }
    
    @ApiOperation(value = "换桌接口", notes = "换桌接口")
    @PutMapping("/{originalTableId}/{targetTableId}/exchange")
    public ResponseData exchange(@PathVariable("originalTableId") Integer originalTableId, @PathVariable("targetTableId") Integer targetTableId) {
        ResponseData<TableWithDishesResponse> responseData = new ResponseData<>(tableService.exchange(originalTableId, targetTableId));
        if (null != responseData.getResult()) {
            responseData.setCode(TableErrorCode.TABLE_EXCHANGE_ERROR.getCode());
            responseData.setMessage(TableErrorCode.TABLE_EXCHANGE_ERROR.getMessage());
        }
        return responseData;
    }
    
    
    @ApiOperation(value = "获取打印集合接口", notes = "获取打印集合接口")
    @GetMapping("/print/{shopId}")
    public ResponseData<?> print(@PathVariable("shopId") Integer shopId) {
        return new ResponseData<>(printService.popMenus(shopId));
    }
}
