package ink.wgink.module.oauth.client.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.UpdateException;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.oauth.client.OauthClientDTO;
import ink.wgink.pojo.dtos.oauth.client.OauthClientSimpleDTO;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.module.oauth.client.dao.IOauthClientDao;
import ink.wgink.module.oauth.client.service.IOauthClientService;
import ink.wgink.util.AesUtil;
import ink.wgink.util.UUIDUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: OauthClientServiceImpl
 * @Description: Oauth客户端
 * @Author: WangGeng
 * @Date: 2019/3/12 11:40 AM
 * @Version: 1.0
 **/
@Service
public class OauthClientServiceImpl extends DefaultBaseService implements IOauthClientService {

    @Autowired
    private IOauthClientDao oauthClientDao;
    // @Autowired
    // private IMenuService menuService;

    @Override
    public SuccessResult save(Map<String, Object> params) {
        setSaveInfo(params);
        oauthClientDao.save(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult remove(Map<String, Object> params) {
        params.put("secretIds", Arrays.asList(params.get("secretIds").toString().split("_")));
        setUpdateInfo(params);
        oauthClientDao.remove(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult update(Map<String, Object> params) {
        setUpdateInfo(params);
        oauthClientDao.update(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResultList<List<OauthClientDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<OauthClientDTO> oauthClientDTOs = oauthClientDao.list(page.getParams());
        PageInfo<OauthClientDTO> pageInfo = new PageInfo<>(oauthClientDTOs);
        return new SuccessResultList<>(oauthClientDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public List<OauthClientDTO> list(Map<String, Object> params) {
        return oauthClientDao.list(params);
    }

    @Override
    public List<OauthClientSimpleDTO> listSimple(Map<String, Object> params) {
        return oauthClientDao.listSimple(params);
    }

    @Override
    public OauthClientDTO get(Map<String, Object> params) throws UnsupportedEncodingException {
        OauthClientDTO oauthClientDTO = oauthClientDao.get(params);
        oauthClientDTO.setClientSecret(Base64.encodeBase64String(AesUtil.aesEncoder(OAUTH_CLIENT_RULE, oauthClientDTO.getClientId()).getBytes("UTF-8")));
        return oauthClientDTO;
    }

    @Override
    public OauthClientDTO getInitClient(Map<String, Object> params) throws UnsupportedEncodingException {
        OauthClientDTO oauthClientDTO = new OauthClientDTO();
        String clientId = UUIDUtil.get32UUID();
        String clientSecret = Base64.encodeBase64String(AesUtil.aesEncoder(OAUTH_CLIENT_RULE, clientId).getBytes("UTF-8"));
        oauthClientDTO.setClientId(clientId);
        oauthClientDTO.setClientSecret(clientSecret);
        oauthClientDTO.setAccessTokenValidity(7200);
        oauthClientDTO.setRefreshTokenValidity(7200);
        return oauthClientDTO;
    }

    @Override
    public synchronized SuccessResult updateOauthClientMenu(Map<String, Object> params) {
        OauthClientDTO oauthClientDTO = oauthClientDao.get(params);
        if (!StringUtils.isBlank(oauthClientDTO.getMenuId())) {
            throw new UpdateException("菜单已经绑定", true);
        }
        if (params.get("menuId") == null || StringUtils.isBlank(params.get("menuId").toString())) {
            LOG.debug("菜单为空，创建菜单");
            // 创建菜单
            Map<String, Object> menuParams = new HashMap<>(16);
            menuParams.put("menuParentId", 0);
            menuParams.put("menuName", oauthClientDTO.getClientName() + "菜单");
            menuParams.put("apiPrefix", "");
            menuParams.put("resourcePrefix", "");
            menuParams.put("routePrefix", "");
            menuParams.put("menuUrl", "javascript:void(0);");
            menuParams.put("menuType", "1");
            menuParams.put("menuIcon", "fa-icon-color-white fa fa-list");
            menuParams.put("menuOrder", "1");
            menuParams.put("menuStatus", "0");
            setSaveInfo(menuParams);
            // menuService.saveMenu(menuParams);
            // 绑定菜单
            params.put("menuId", menuParams.get("menuId"));
        }
        oauthClientDao.update(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResultData<Integer> count(Map<String, Object> params) {
        Integer oauthClientCount = oauthClientDao.count(params);
        return new SuccessResultData<>(oauthClientCount == null ? 0 : oauthClientCount);
    }

    @Override
    public SuccessResult updateClientMenuEmpty(Map<String, Object> params) {
        oauthClientDao.updateMenuEmpty(params);
        return new SuccessResult();
    }

    @Override
    public OauthClientDTO getSimple(String clientId) {
        Map<String, Object> params = getHashMap(2);
        params.put("clientId", clientId);
        return oauthClientDao.getSimple(params);
    }
}
