package com.ycwl.basic.controller.mobile;

import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.pc.printer.resp.MemberPrintResp;
import com.ycwl.basic.model.pc.printer.resp.PrinterResp;
import com.ycwl.basic.model.printer.req.FromSourceReq;
import com.ycwl.basic.service.printer.PrinterService;
import com.ycwl.basic.storage.StorageFactory;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/mobile/printer/v1")
public class AppPrinterController {
    @Autowired
    private PrinterService printerService;
    @GetMapping("/listFor/{scenicId}")
    @IgnoreToken
    public ApiResponse<List<PrinterResp>> listFor(@PathVariable("scenicId") Long scenicId) {
        return ApiResponse.success(printerService.listByScenicId(scenicId));
    }

    @GetMapping("/getListFor/{scenicId}")
    public ApiResponse<List<MemberPrintResp>> getListFor(@PathVariable("scenicId") Long scenicId) {
        JwtInfo worker = JwtTokenUtil.getWorker();
        return ApiResponse.success(printerService.getUserPhotoList(worker.getUserId(), scenicId));
    }

    @GetMapping("/getItem/{scenicId}/{id}")
    public ApiResponse<MemberPrintResp> getItem(@PathVariable("scenicId") Long scenicId, @PathVariable("id") Long id) {
        JwtInfo worker = JwtTokenUtil.getWorker();
        MemberPrintResp userPhoto = printerService.getUserPhoto(worker.getUserId(), scenicId, id);
        if (userPhoto == null) {
            return ApiResponse.fail("未找到该图片");
        }
        return ApiResponse.success(userPhoto);
    }

    @PostMapping("/deleteFrom/{scenicId}/{id}")
    public ApiResponse<?> deleteFrom(@PathVariable("scenicId") Long scenicId, @PathVariable("id") Long id) throws IOException {
        JwtInfo worker = JwtTokenUtil.getWorker();
        printerService.deleteUserPhoto(worker.getUserId(), scenicId, id);
        return ApiResponse.success(null);
    }
    @PostMapping("/uploadTo/{scenicId}")
    public ApiResponse<?> upload(@PathVariable("scenicId") Long scenicId, @RequestParam(value = "file") MultipartFile file) throws IOException {
        String[] split = file.getOriginalFilename().split("\\.");
        String ext = split[split.length - 1];
        String url = StorageFactory.use().uploadFile(file, "printer", UUID.randomUUID() + "." + ext);
        printerService.addUserPhoto(JwtTokenUtil.getWorker().getUserId(), scenicId, url);
        return ApiResponse.success(url);
    }
    @PostMapping("/uploadTo/{scenicId}/cropped/{id}")
    public ApiResponse<?> uploadReplace(@PathVariable("scenicId") Long scenicId, @PathVariable("id") Long id, @RequestParam(value = "file") MultipartFile file) throws IOException {
        String[] split = file.getOriginalFilename().split("\\.");
        String ext = split[split.length - 1];
        String url = StorageFactory.use().uploadFile(file, "printer", UUID.randomUUID() + "." + ext);
        printerService.setPhotoCropped(JwtTokenUtil.getWorker().getUserId(), scenicId, id, url);
        return ApiResponse.success(url);
    }
    @PostMapping("/uploadTo/{scenicId}/formSource")
    public ApiResponse<?> uploadFromSource(@PathVariable("scenicId") Long scenicId, @RequestBody FromSourceReq req) throws IOException {
        printerService.addUserPhotoFromSource(JwtTokenUtil.getWorker().getUserId(), scenicId, req);
        return ApiResponse.success(null);
    }

    @PostMapping("/setQuantity/{scenicId}/{id}")
    public ApiResponse<?> setQuantity(@PathVariable("scenicId") Long scenicId, @PathVariable("id") Long id, @RequestParam("quantity") Integer quantity) {
        if (quantity == null) {
            return ApiResponse.fail("请输入数量");
        }
        if (quantity < 0) {
            return ApiResponse.fail("数量不能小于0");
        }
        printerService.setPhotoQuantity(JwtTokenUtil.getWorker().getUserId(), scenicId, id, quantity);
        return ApiResponse.success(null);
    }
    @GetMapping("/price/{scenicId}")
    public ApiResponse<?> queryPrice(@PathVariable("scenicId") Long scenicId) {
        return ApiResponse.success(printerService.queryPrice(JwtTokenUtil.getWorker().getUserId(), scenicId));
    }

    @PostMapping("/order/{scenicId}")
    public ApiResponse<Map<String, Object>> createOrder(@PathVariable("scenicId") Long scenicId) {
        return ApiResponse.success(printerService.createOrder(JwtTokenUtil.getWorker().getUserId(), scenicId, null));
    }
    @PostMapping("/order/{scenicId}/toPrinter/{printerId}")
    public ApiResponse<Map<String, Object>> createOrderToPrinter(@PathVariable("scenicId") Long scenicId, @PathVariable("printerId") Integer printerId) {
        return ApiResponse.success(printerService.createOrder(JwtTokenUtil.getWorker().getUserId(), scenicId, printerId));
    }
}
