/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.webapi.api.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.commoncore.utils.DecPwdUtil;
import com.codvision.commoncore.utils.ResponseUtil;
import com.codvision.webapi.api.SysUserApi;
import com.codvision.webapi.bean.RemoteOrg;
import com.codvision.webapi.bean.RemoteSysUser;
import com.codvision.webapi.bean.RemoteUserOrgRole;
import com.codvision.webapi.common.EncryptUtil;
import com.codvision.webapi.common.ListResponseEntity;
import com.codvision.webapi.remote.RemoteOrgService;
import com.codvision.webapi.remote.RemoteUserService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户接口实现
 *
 * @author lingee
 * @date 2023/7/27
 */
public class SysUserApiImpl implements SysUserApi {

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private RemoteOrgService remoteOrgService;

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public IPage<RemoteSysUser> pageUserList(String keyword, Integer oid, Integer roleId, List<Integer> uidList, int pageNum, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("oid", oid);
        map.put("role", roleId);
//        map.put("uidList", uidList);
        map.put("page", pageNum);
        map.put("size", pageSize);

        ListResponseEntity<RemoteSysUser> responseList;
        try {
            String encryption = EncryptUtil.encoderBody(map);
            ResponseEntity<String> response = remoteUserService.pageUserList(encryption);
            if (!ResponseUtil.isSuccess(response)) {
                return null;
            }

            responseList = objectMapper.readValue(EncryptUtil.decoderBody(response.getData()),
                    new TypeReference<ListResponseEntity<RemoteSysUser>>() {
                    });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        IPage<RemoteSysUser> page = new Page(responseList.getPage(), responseList.getSize(), responseList.getTotal());
        page.setRecords(responseList.getList());
        return page;
    }

    @Override
    public List<RemoteSysUser> getAllUserList(String keyword, Integer oid, Integer roleId) {
        IPage<RemoteSysUser> pageResult = pageUserList(keyword, oid, roleId, null, 1, 100000);
        return pageResult.getRecords();
    }

    @Override
    public IPage<RemoteOrg> pageOrgList(String keyword, Boolean isAll, int pageNum, int pageSize) {
        ResponseEntity<ListResponseEntity<RemoteOrg>> response
                = remoteOrgService.pageOrgList(keyword, isAll, pageNum, pageSize);
        if (!ResponseUtil.isSuccess(response)) {
            return null;
        }
        ListResponseEntity<RemoteOrg> responseList = response.getData();
        IPage<RemoteOrg> page = new Page(responseList.getPage(), responseList.getSize(), responseList.getTotal());
        page.setRecords(responseList.getList());
        return page;
    }

    @Override
    public Integer addUser(RemoteSysUser sysUser) {
        Map<String, Object> map = new HashMap<>();
        sysUser.setPassword(DecPwdUtil.encryptAes(sysUser.getPassword()));
        map.put("encryption", EncryptUtil.encoderBody(sysUser));

        ResponseEntity<String> response = remoteUserService.add(map);
        if (!ResponseUtil.isSuccess(response)) {
            throw new BusinessException(response.getMessage());
        }
        try {
            return Integer.parseInt(objectMapper.readValue(EncryptUtil.decoderBody(response.getData()), String.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public RemoteSysUser getUserDetail(Integer uid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);

        try {
            String encryption = EncryptUtil.encoderBody(map);
            ResponseEntity<String> response = remoteUserService.getByUid(encryption);
            if (!ResponseUtil.isSuccess(response)) {
                return null;
            }
            RemoteSysUser sysUser = objectMapper.readValue(EncryptUtil.decoderBody(response.getData()), RemoteSysUser.class);
            return sysUser;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public RemoteSysUser getUserDetail(String loginName) {
        Map<String, Object> map = new HashMap<>();
        map.put("loginName", loginName);

        try {
            String encryption = EncryptUtil.encoderBody(map);
            ResponseEntity<String> response = remoteUserService.getByLoginName(encryption);
            if (!ResponseUtil.isSuccess(response)) {
                return null;
            }
            RemoteSysUser sysUser = objectMapper.readValue(EncryptUtil.decoderBody(response.getData()), RemoteSysUser.class);
            return sysUser;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateUserOrgRole(Integer uid, Integer oid, String roleCode) {
        RemoteUserOrgRole userOrgRole = new RemoteUserOrgRole(uid, oid, null, roleCode);
        RemoteSysUser sysUser = new RemoteSysUser();
        sysUser.setId(uid);
        sysUser.setUserOrgRoles(Arrays.asList(userOrgRole));
        Map<String, Object> map = new HashMap<>();
        map.put("encryption", EncryptUtil.encoderBody(sysUser));

        ResponseEntity<String> response = remoteUserService.update(map);
        if (!ResponseUtil.isSuccessNotData(response)) {
            throw new BusinessException(response.getMessage());
        }
    }
}
