package io.hmit.controller;

import ch.qos.logback.core.pattern.ConverterUtil;
import com.alibaba.fastjson.JSON;
import io.hmit.annotation.Login;
import io.hmit.annotation.LoginUser;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.common.utils.Result;
import io.hmit.dto.OrgUserInfoDTO;
import io.hmit.dto.ServiceUserInfoDTO;
import io.hmit.dto.UserInfoDTO;
import io.hmit.dto.UserRegisterDTO;
import io.hmit.entity.UserEntity;
import io.hmit.modules.serviceOrder.dto.PensionOldFamilyDTO;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.modules.serviceOrder.dto.PensionOrderEvaluationDTO;
import io.hmit.modules.serviceOrder.dto.UserAddressDTO;
import io.hmit.modules.serviceOrder.service.PensionOldFamilyService;
import io.hmit.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @Description :
 * @Author : Shen Yuanfeng
 * @Date: 2021/3/17 16:35
 */

@Slf4j
@RestController
@RequestMapping("api/user")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PensionOldFamilyService pensionOldFamilyService;

    @Login
    @GetMapping("oldinfo")
    @ApiOperation("老人个人信息")
    public Result<UserInfoDTO> getOldInfo(@ApiIgnore @LoginUser UserEntity user){
        UserInfoDTO userDetails = userService.getUserDetails(user.getId());
        return new Result<UserInfoDTO>().ok(userDetails);
    }

    @Login
    @GetMapping("oldfamilyinfo")
    @ApiOperation("老人家属信息")
    public Result<List<PensionOldFamilyDTO>> getOldFamilyInfo(@ApiIgnore @LoginUser UserEntity user){
        List<PensionOldFamilyDTO> familyDTOS = pensionOldFamilyService.findByOldId(user.getId());
        return new Result<List<PensionOldFamilyDTO>>().ok(familyDTOS);
    }

    @Login
    @GetMapping("orginfo")
    @ApiOperation("机构信息")
    public Result<OrgUserInfoDTO> getOrgUserInfo(@ApiIgnore @LoginUser UserEntity user){
        OrgUserInfoDTO orgUserInfo = userService.getOrgUserByUserId(user.getId());
        return new Result<OrgUserInfoDTO>().ok(orgUserInfo);
    }

    @Login
    @GetMapping("serviceinfo")
    @ApiOperation("服务人员信息")
    public Result<ServiceUserInfoDTO> serviceinfo(@ApiIgnore @LoginUser UserEntity user){
        ServiceUserInfoDTO serviceUser = userService.getServiceUserByUserId(user.getId());
        return new Result<ServiceUserInfoDTO>().ok(serviceUser);
    }

    @GetMapping("checkIdCardNo")
    @ApiOperation("注册身份证ID查询")
    public Result<UserRegisterDTO> checkIdCardNo(@RequestParam("idCardNo") Long idCardNo){
        UserRegisterDTO userRegisterDTO = userService.getUserInfoByIdCardNo(idCardNo);
        return new Result<UserRegisterDTO>().ok(userRegisterDTO);
    }

    @PostMapping("registerUser")
    @ApiOperation("注册用户")
    public Result registerUser(@RequestBody String dto){
        UserRegisterDTO userRegisterDTO = null;
        try {
            userRegisterDTO = JSON.parseObject(URLDecoder.decode(dto,"UTF-8"), UserRegisterDTO.class);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        userService.registerUser(userRegisterDTO);
        return new Result();
    }

    @Login
    @GetMapping("findUserAdderss")
    @ApiOperation("根据手机号或用户姓名查找用户地址")
    public Result<List<UserAddressDTO>> findUserAdderss(@RequestParam("str") String str){
        if (str.trim().isEmpty()) {
            return new Result<List<UserAddressDTO>>().ok(null);
        }
        List<UserAddressDTO> userAddressDTOList = userService.findUserAddressByUsernameOrMobile(str);
        return new Result<List<UserAddressDTO>>().ok(userAddressDTOList);
    }

}
