package cn.ysk.cashier.controller;

import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.dto.calltable.*;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.service.app.TbCallService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 叫号
 */
@RestController
@RequestMapping("/callTable")
@AllArgsConstructor
public class TbCallTableController {

    private final TbCallService tbCallService;

    @AnonymousAccess
    @GetMapping
    public ResponseEntity<?> get(
            @RequestParam(required = false) Integer callTableId,
            @RequestParam Integer shopId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer state
    ) {
        return ResponseEntity.ok(tbCallService.get(page, size, shopId, callTableId, state));
    }

    @AnonymousAccess
    @PostMapping
    public ResponseEntity<?> add(
            @Validated @RequestBody CallTableDTO addCallTableDTO
    ) {
        return ResponseEntity.ok(tbCallService.add(addCallTableDTO));
    }

    @AnonymousAccess
    @PutMapping
    public ResponseEntity<?> update(
            @Validated @RequestBody UpdateCallTableDTO callTableDTO
    ) {
        return ResponseEntity.ok(tbCallService.update(callTableDTO));
    }

    @AnonymousAccess
    @DeleteMapping
    public ResponseEntity<?> delete(
            @Validated @RequestBody BaseCallTableDTO baseCallTableDTO
    ) {
        return ResponseEntity.ok(tbCallService.delete(baseCallTableDTO));
    }

    @AnonymousAccess
    @PostMapping("takeNumber")
    public ResponseEntity<?> takeNumber(
            @Validated @RequestBody TakeNumberDTO takeNumberDTO
    ) {
        return ResponseEntity.ok(tbCallService.takeNumber(takeNumberDTO));
    }

    @AnonymousAccess
    @GetMapping("takeNumberCode")
    public ResponseEntity<?> takeNumberCode(
            @RequestParam Integer shopId,
            @RequestParam Integer callTableId
    ) {
        return ResponseEntity.ok(tbCallService.takeNumberCode(shopId, callTableId));
    }

    @AnonymousAccess
    @PostMapping("call")
    public ResponseEntity<?> call(
            @Validated @RequestBody CallQueueDTO callQueueDTO
    ) {
        return ResponseEntity.ok(tbCallService.call(callQueueDTO));
    }

    @AnonymousAccess
    @PutMapping("updateState")
    public ResponseEntity<?> confirm(
            @Validated @RequestBody UpdateCallQueueDTO updateCallQueueDTO
    ) {
        return ResponseEntity.ok(tbCallService.updateInfo(updateCallQueueDTO));
    }

    @AnonymousAccess
    @GetMapping("queue")
    public ResponseEntity<?> getQueue(
            @RequestParam Integer shopId,
            @RequestParam(required = false) Integer callTableId,
            @RequestParam(required = false) Integer state,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        return ResponseEntity.ok(tbCallService.getQueue(shopId, callTableId, state, page, size));
    }


}
