package jnpf.attendance.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import jnpf.attendance.service.AttendanceBaseSettingService;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.exception.HandleException;
import jnpf.model.attendance.dto.AttendanceBaseSettingDto;
import jnpf.model.attendance.vo.AttendanceBaseSettingVo;
import jnpf.model.attendance.vo.attendance.QuickCheckInVo;
import jnpf.util.ConstantUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 考勤基础设置表 前端控制器
 *
 * @author ahua
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/base/setting")
public class AttendanceBaseSettingController {
    @Autowired
    private AttendanceBaseSettingService attendanceBaseSettingService;

    /**
     * 保存考勤基础设置信息。
     *
     * @param attendanceBaseSettingDto 考勤基础设置数据传输对象
     * @throws HandleException            处理异常
     * @return 处理结果视图对象
     */
    @PostMapping
    public ActionResult<Void> save(@RequestBody AttendanceBaseSettingDto attendanceBaseSettingDto) throws HandleException {
        attendanceBaseSettingService.save(attendanceBaseSettingDto);
        return ActionResult.success();
    }

    /**
     * 根据考勤组ID获取单个考勤基础设置。
     *
     * @param groupId 考勤组ID
     * @return 考勤基础设置视图对象
     */
    @GetMapping("{groupId}")
    public ActionResult<AttendanceBaseSettingVo> getOne(@PathVariable String groupId) {
        return ActionResult.success(attendanceBaseSettingService.getOne(groupId));
    }

    /**
     * 更改考勤基础设置的启用状态。
     *
     * @param groupId 考勤组ID
     * @param enable  启用状态（0禁用，1启用）
     * @return 处理结果视图对象
     */
    @PutMapping
    public ActionResult<Void> changeStatus(@RequestParam String groupId, @RequestParam Integer enable) {
        attendanceBaseSettingService.changeStatus(groupId, enable, attendanceBaseSettingService.getOne(groupId));
        return ActionResult.success();
    }

    /**
     * 查询用户外勤是否必须拍照
     * @param isFromOaCondition 是否来自OA条件 是需要返回所有可能选项
     * @return java.lang.Object
     */
    @GetMapping(value = "/takePhoto")
    public Object getTakePhotoSetting(@RequestParam(required = false) boolean isFromOaCondition) throws Exception {
        if (isFromOaCondition) {
            JSONArray array = new JSONArray();
            JSONObject json = new JSONObject();
            json.set("state", ConstantUtil.NUM_TRUE);
            json.set("name", "是");
            array.put(json);
            JSONObject json1 = new JSONObject();
            json1.set("state", ConstantUtil.NUM_FALSE);
            json1.set("name", "否");
            array.put(json1);
            return ActionResult.success(array);
        }
        UserInfo user = UserProvider.getUser();
        JSONArray shouldTakePhoto = attendanceBaseSettingService.getTakePhotoSetting(user);
        return ActionResult.success(shouldTakePhoto);
    }

    /**
     * 查询用户外勤是否必须拍照
     * @return java.lang.Object
     */
    @GetMapping(value = "/takePhoto2")
    public Object getTakePhotoSetting2() throws Exception {

        UserInfo user = UserProvider.getUser();
        JSONArray shouldTakePhoto = attendanceBaseSettingService.getTakePhotoSetting(user);
        return ActionResult.success(shouldTakePhoto.get(0));
    }


    /**
     * 获取用户当前时间所处考勤组的内勤打卡基础设置。（过度使用）
     */
    @GetMapping("/getNowGroupAttendancePhoto")
    public ActionResult<Integer> getNowGroupAttendancePhoto() {
        return ActionResult.success(attendanceBaseSettingService.getNowGroupAttendancePhoto());
    }

    /**
     * 获取用户当前时间所处考勤组是否能快速打卡
     * 和用户当前所处考勤组的基础设置中的是否需要拍照，是否需要人脸识别以及App个人设置中的上下班快速打开相关
     */
    @GetMapping("/quickCheckIn")
    public ActionResult<QuickCheckInVo> quickCheckIn() {
        return ActionResult.success(attendanceBaseSettingService.quickCheckIn());
    }
}

