package com.jpsoft.shinestar.modules.mobile.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.Person;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.dto.IconInfoDTO;
import com.jpsoft.shinestar.modules.base.dto.PersonPopedomDTO;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.JwtUtil;
import com.jpsoft.shinestar.modules.common.utils.OSSUtil;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 我的亲属
 * shuzhan
 */
@RestController
@RequestMapping("/mobile/relationApi")
@Slf4j
public class RelationApiController {

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonRoleInfoService personRoleInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;


    @PostMapping("relationList")
    @ApiOperation(value = "亲属列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "query", dataType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标", paramType = "form")
    })
    public MessageResult<Map> relationList(
            String token, @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.get(new Long(subject));
            List<Map> relationsList = new ArrayList<>();
            //查身为长者的
            List<PersonPopedom> ppList = personPopedomService.findByRelationPopedomId(personInfo.getPopedom());
            for(PersonPopedom pp : ppList){
                //关联的用户
                PersonInfo relativePi = personInfoService.get(pp.getPersonId());
                Map<String, Object> map = new HashMap<>();
                map.put("popedomId",pp.getId());
                map.put("relationName",pp.getRelationName());//关系
                map.put("name",relativePi.getName());//姓名
                map.put("phone",relativePi.getPhone());//电话
                map.put("faceImageUrl",relativePi.getFaceImageUrl());//头像
                relationsList.add(map);
            }

            //亲属的
            PersonPopedom relationPp = personPopedomService.get(personInfo.getPopedom());
            List<PersonPopedom> relationPpList = personPopedomService.findByRelationPopedomId(relationPp.getRelationPopedom());
            for(PersonPopedom pp : relationPpList){
                //关联的用户
                PersonInfo relativePi = personInfoService.get(pp.getPersonId());
                if(relativePi.getId().longValue() == personInfo.getId().longValue()){
                    continue;
                }
                Map<String, Object> map = new HashMap<>();
                map.put("popedomId",pp.getId());
                map.put("relationName",pp.getRelationName());//关系
                map.put("name",relativePi.getName());//姓名
                map.put("phone",relativePi.getPhone());//电话
                map.put("faceImageUrl",relativePi.getFaceImageUrl());//头像
                relationsList.add(map);
            }

            PersonPopedom relationPopedom = personPopedomService.get(relationPp.getRelationPopedom());
            if(relationPopedom != null) {
                PersonInfo relationPerson = personInfoService.get(relationPopedom.getPersonId());
                Map<String, Object> relationMap = new HashMap<>();
                relationMap.put("popedomId", relationPopedom.getId());
                relationMap.put("relationName", relationPp.getRelationName());//关系
                relationMap.put("name", relationPerson.getName());//姓名
                relationMap.put("phone", relationPerson.getPhone());//电话
                relationMap.put("faceImageUrl", relationPerson.getFaceImageUrl());//头像
                relationsList.add(0,relationMap);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("relationsList", relationsList);
            messageResult.setData(map);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("findByPopedomId")
    @ApiOperation(value = "通过popedomId查询家属")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "popedomId", value = "关联关系ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "query", dataType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标", paramType = "form")
    })
    public MessageResult<Map> findByPopedomId(
                              @RequestParam(value="popedomId",defaultValue="") String popedomId,
                              String token,
                              @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            Map<String, Object> personMap = new HashMap<String, Object>();
            PersonPopedom personPopedom = personPopedomService.get(popedomId);

            if (personPopedom == null) {
                messageResult.setResult(false);
                messageResult.setMessage("数据库不存在该记录!");
                return messageResult;
            }

            PersonInfo personInfo = personInfoService.get(personPopedom.getPersonId());
            boolean delStatus = false;

            PersonPopedom pp = personPopedomService.get(personPopedom.getRelationPopedom());
            if(pp != null && new Long(subject).longValue() == pp.getPersonId().longValue()){
                //如果是自己的角色,不能删除
                delStatus = true;
            }

            personMap.put("popedomId",personInfo.getId());//关联关系ID
            personMap.put("name",personInfo.getName());//姓名
            personMap.put("phone",personInfo.getPhone());//手机号
            personMap.put("idCard",personInfo.getIdCard());//身份证
            personMap.put("address",personPopedom.getAddress());//联系地址
            personMap.put("relationName",personPopedom.getRelationName());//亲属关系
            personMap.put("faceImageUrl",personInfo.getFaceImageUrl());//照片
            personMap.put("delStatus",delStatus);


            Map<String, Object> dataMap = new HashMap<String, Object>();

            dataMap.put("person", personMap);

            messageResult.setData(dataMap);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }


    @PostMapping("clearRelation")
    @ApiOperation(value="清除亲属关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "popedomId", value = "关联关系ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "query", dataType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标", paramType = "form")
    })
    public MessageResult<String> clearRelation(
            String popedomId, String token, @RequestAttribute  String subject){
        MessageResult<String> messageResult = new MessageResult<>();

        try {
            PersonPopedom relativePp = personPopedomService.get(popedomId);

            if (relativePp==null){
                throw new Exception("人员信息不存在!");
            }

//            relativePp.setRelationName("");
//            relativePp.setRelationPopedom("");
            relativePp.setDelFlag(true);
            relativePp.setUpdateBy(subject);
            relativePp.setUpdateTime(new Date());
            int updateCount = personPopedomService.update(relativePp);

            messageResult.setResult(true);
            messageResult.setData("操作成功");
            messageResult.setCode(200);
        } catch (Exception e) {
            log.error(e.getMessage(),e);

            messageResult.setResult(false);
            messageResult.setMessage(e.getMessage());
        }

        return messageResult;
    }
}
