package com.gyt.shr.controller.UserController;


import com.gyt.shr.entity.CodeRecord;
import com.gyt.shr.service.CodeRecordService;
import com.gyt.shr.service.PersonService;
import com.gyt.shr.util.commonUtils;
import com.gyt.shr.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.gyt.shr.constant.GlobalConstant.NORMAL_USER_LOGIN_STATUS;


/**
 * 身份验证控制器
 * coyle
 * 20250121
 */

@Controller
@CrossOrigin //  允许跨域
public class IdentityController {
    @Autowired
    private PersonService personService;
    @Autowired
    private CodeRecordService  codeRecordService;


    /**
     * 检查验证码
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/normalUser/identity")
    public String recommend(Model model, HttpServletRequest request, @RequestParam  String phone, @RequestParam String code) {
        Result<Boolean> booleanResult = codeRecordService.checkHasCode(phone, code, 1);
        if (booleanResult.getCode() == 200 ){
            return "normalUser/identity.html";
        }else{
            return "redirect:/";
        }

    }

    /**
     * 身份证提交并验证
     * @param mapData
     * @param request
     * @return
     */
    @PostMapping("/normalUser/idSubmit")
    public ResponseEntity<?>  idSubmit(@RequestBody Map<String, Object> mapData, HttpServletRequest request) {
        String idNo = (String) mapData.get("idNo");
        String code = (String) mapData.get("code");
        Result<List<CodeRecord>> result = codeRecordService.checkIdNo(request, idNo, code, 1);
        if (result.getCode() == 200 && result.getData().size() > 0){
            List<CodeRecord> data = result.getData();
            String pid = data.get(0).getPid();
            return ResponseEntity.ok(commonUtils.createImmutableMap("success", true, "message", result.getMsg(),"pid", pid));
        }
        return ResponseEntity.ok(commonUtils.createImmutableMap("success", false, "message", result.getMsg()));
    }

    /**
     * @return 返回视图信息。
     * @description 用于管理员登录：普通管理员和超级管理员
     */
    @PostMapping("/person/idNoLogin")
    public ResponseEntity<?> idNoLogin(@RequestBody Map<String, Object> loginData, HttpServletRequest request) {
        String pnumber = (String) loginData.get("pnumber");
        String idNo = (String) loginData.get("idNo");
        if (request.getSession().getAttribute(NORMAL_USER_LOGIN_STATUS) != null) {
            Map<String, Object> responseMap = new HashMap<>();
            responseMap.put("success", true);
            responseMap.put("message", "已登录");
            responseMap.put("redirectTo", "/admin/adminUser/index");
            return ResponseEntity.ok(createImmutableMap(responseMap));
        } else {
            Result<Integer> result = personService.personLogin(pnumber, idNo, request);
            // 假设这里需要处理登录结果并返回相应的响应
            if (result.getSuccess()) {
                Map<String, Object> responseMap = new HashMap<>();
                responseMap.put("success", true);
                responseMap.put("message", "成功");
                return ResponseEntity.ok(createImmutableMap(responseMap));
            } else {
                Map<String, Object> responseMap = new HashMap<>();
                responseMap.put("success", false);
                responseMap.put("message", "登录失败");
                return ResponseEntity.ok(createImmutableMap(responseMap));
            }
        }
    }

    private Map<String, Object> createImmutableMap(Map<String, Object> map) {
        return Collections.unmodifiableMap(map);
    }

}
