package cn.iocoder.yudao.module.member.controller.app.diningplates;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.diningplates.vo.DiningPlatesRespVO;
import cn.iocoder.yudao.module.member.controller.admin.diningplates.vo.DiningPlatesSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.diningplates.vo.AppUserInfo;
import cn.iocoder.yudao.module.member.controller.app.diningplates.vo.BindVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppUserInfoCardVO;
import cn.iocoder.yudao.module.member.dal.dataobject.diningplates.DiningPlatesDO;
import cn.iocoder.yudao.module.member.service.diningplates.DiningPlatesService;
import cn.iocoder.yudao.module.system.api.deviceInfo.DeviceInfoApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 餐盘")
@RestController
@RequestMapping("/member/dining-plates")
@Validated
public class AppDiningPlatesController {

    @Resource
    private DiningPlatesService diningPlatesService;

    @Resource
    private HttpServletRequest httpServletRequest;

    @Resource
    private DeviceInfoApi deviceInfoApi;

    @GetMapping("/list")
    @Operation(summary = "获得餐盘列表")
    public CommonResult<List<DiningPlatesRespVO>> getDiningPlatesPage() {
        List<DiningPlatesDO> pageResult = diningPlatesService.getDiningPlatesList();
        return success(BeanUtils.toBean(pageResult, DiningPlatesRespVO.class));
    }

    @GetMapping("/bindByCard")
    @Operation(summary = "打卡绑定餐盘")
    public CommonResult<AppUserInfoCardVO> bindByCard(String diningPlatesNum,String cardId,Long storeId) {
        return success(diningPlatesService.bind(diningPlatesNum,cardId,storeId));
    }

    @PostMapping("/bindByFace")
    @Operation(summary = "人脸绑定餐盘")
    public CommonResult<AppUserInfoCardVO> bindByFace(@RequestBody BindVO vo) {
        return success(diningPlatesService.bindByFace(vo.getDiningPlatesNum(), vo.getFaceId(), vo.getStoreId(), vo.getMobile(),vo.getFaceData()));
    }
    
    @GetMapping("/bindByFace")
    @Operation(summary = "人脸绑定餐盘")
    public CommonResult<AppUserInfoCardVO> bindByFace(String diningPlatesNum, Long faceId,Long storeId,String mobile) {
        return success(diningPlatesService.bindByFace(diningPlatesNum,faceId,storeId,mobile,null));
    }

    @GetMapping("/checkBind")
    @Operation(summary = "验证餐盘绑定")
    public CommonResult<AppUserInfoCardVO> checkBind(String diningPlatesNum, Long storeId) {
        Long newStoreId  = getCarteen(storeId);
        return success(diningPlatesService.appCheckBind(diningPlatesNum,newStoreId));
    }

    @GetMapping("/unbind")
    @Operation(summary = "餐盘解绑")
    public CommonResult<String> unbind(String diningPlatesNum,Long storeId) {
        Long newStoreId  = getCarteen(storeId);
        diningPlatesService.unbind(diningPlatesNum,newStoreId);
        return success(diningPlatesNum);
    }

    @GetMapping("/getUserAndDish")
    @Operation(summary = "根据餐盘号获取当前余额")
    public CommonResult<AppUserInfo> getMoney(String cId, Long dishesId,Long storeId) {
        Long newStoreId   = getCarteen(storeId);
        return success(diningPlatesService.getMoney(cId,dishesId,newStoreId));
    }

    @GetMapping("/getBind")
    @Operation(summary = "获取绑定餐盘验证")
    public CommonResult<List<String>> getBind() {
        return success(diningPlatesService.getBindDiningPlatesList());
    }

    @GetMapping("/checkBindAndUnBind")
    @Operation(summary = "餐盘解绑")
    public CommonResult<String> checkBindAndUnBind(String diningPlatesNum,Long storeId) {
        Long newStoreId  = getCarteen(storeId);
        Map<String, String> map = diningPlatesService.checkBindAndUnBind(diningPlatesNum, newStoreId);
        CommonResult<String> result = new CommonResult<>();
        result.setCode(200);
        result.setData(map.get("data"));
        result.setMsg(map.get("msg"));
        return result;
    }

    @PostMapping("/create")
    @Operation(summary = "创建餐盘")
    public CommonResult<Long> createDiningPlates(@Valid @RequestBody DiningPlatesSaveReqVO createReqVO) {
        return success(diningPlatesService.createDiningPlates(createReqVO));
    }


    public Long getCarteen(Long storeId) {
        if (storeId != null && storeId != 0){
            return storeId;
        }
        try {
            String authorization = httpServletRequest.getHeader("Authorization");
            Long carteen = deviceInfoApi.getCarteen(authorization);

            return carteen;
        } catch (Exception e) {
            throw exception(2000_10_001, "无法获取设备编码");
        }
    }
}