package com.dxhy.auth.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.auth.bean.ClientInfo;
import com.dxhy.auth.dao.ClientDao;
import com.dxhy.auth.entity.ClientEntity;
import com.dxhy.auth.service.AuthClientService;
import com.dxhy.auth.util.client.ClientTokenUtil;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.util.UUIDUtils;

/**
 * @author jiaohongyang
 * @date 2019年3月26日 下午5:55:43
 *
 */
@Service("authClientService")
public class AuthClientServiceImpl implements AuthClientService {
    @Resource
    private ClientDao clientMapper;
    @Resource
    private ClientTokenUtil clientTokenUtil;
    @Resource
    private DiscoveryClient discovery;

    @Override
    public String apply(String clientId, String secret) throws Exception {
        ClientEntity client = getClient(clientId, secret);
        if (client == null || !client.getSecret().equals(secret)) {
            return null;
        } else {
            return clientTokenUtil
                .generateToken(new ClientInfo(client.getCode(), client.getName(), client.getId().toString()));
        }
    }

    private ClientEntity getClient(String clientId, String secret) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ClientEntity client = new ClientEntity();
        client.setCode(clientId);
        client = clientMapper.selectOne(new QueryWrapper<>(client));
        return client;
    }

    @Override
    public void validate(String clientId, String secret) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ClientEntity client = new ClientEntity();
        client.setCode(clientId);
        clientMapper.selectOne(new QueryWrapper<>(client));
    }

    @Override
    public List<String> getAllowedClient(String clientId, String secret) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ClientEntity info = this.getClient(clientId, secret);
        List<String> clients = clientMapper.selectAllowedClient(info.getId() + "");
        if (clients == null) {
            new ArrayList<String>();
        }
        return clients;
    }

    @Override
    public List<String> getAllowedClient(String serviceId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ClientEntity info = getClient(serviceId);
        List<String> clients = clientMapper.selectAllowedClient(info.getId() + "");
        if (clients == null) {
            new ArrayList<String>();
        }
        return clients;
    }

    private ClientEntity getClient(String clientId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ClientEntity client = new ClientEntity();
        client.setCode(clientId);
        client = clientMapper.selectOne(new QueryWrapper<>(client));
        return client;
    }

    @Override
    @Scheduled(cron = "0 0/1 * * * ?")
    public void registryClient() {
        // 自动注册节点
        discovery.getServices().forEach((name) -> {
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            ClientEntity client = new ClientEntity();
            client.setName(name);
            client.setCode(name);
            ClientEntity dbClient = clientMapper.selectOne(new QueryWrapper<>(client));
            if (dbClient == null) {
                client.setSecret(UUIDUtils.generateShortUuid());
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);

                clientMapper.insert(client);
            }
        });
    }
}
