package com.pingcap.syngmail.service.impl;

import cn.authing.core.graphql.GraphQLException;
import cn.authing.core.mgmt.ManagementClient;
import cn.authing.core.types.*;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.services.admin.directory.Directory;
import com.google.api.services.admin.directory.model.Group;
import com.google.api.services.admin.directory.model.Groups;
import com.google.api.services.admin.directory.model.User;
import com.mysql.cj.util.StringUtils;
import com.pingcap.syngmail.common.Common;
import com.pingcap.syngmail.mapper.EmployeeMapper;
import com.pingcap.syngmail.pojo.EmployeeDTO;
import com.pingcap.syngmail.service.EmployeeService;
import com.pingcap.syngmail.thread.*;
import com.pingcap.syngmail.util.AuthUtils;
import com.pingcap.syngmail.util.CommonUtils;
import com.pingcap.syngmail.util.GoogleApiUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zhuxiaopeng
 * @version 1.0.0
 * @Description 员工服务类实现类
 * @createTime 2022年04月08日 14:18:00
 */
@Service
@Slf4j
@EnableScheduling
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${authing.id}")
    private String userPoolId;

    @Value("${authing.secret}")
    private String userPoolSecret;

    @Value("${authing.publicKey}")
    private String publicKey;

    @Value("${authing.url}")
    private String privateUrl;

    @Autowired
    private ThreadPoolExecutor executor;

    /**
     * @author zhuxiaopeng
     * @description 邮件组同步任务
     */
    @Override
    @Scheduled(cron = "0 0 8,12 * * *")
    public void synGmailInfo() {
        try {
            final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            Credential credentials = AuthUtils.getCredentials(HTTP_TRANSPORT);
            credentials.refreshToken();
            Directory service = new Directory.Builder(HTTP_TRANSPORT, AuthUtils.JSON_FACTORY, credentials)
                    .setApplicationName(AuthUtils.APPLICATION_NAME)
                    .build();
            ManagementClient managementClient = new ManagementClient(userPoolId, userPoolSecret);
            managementClient.setPublicKey(publicKey);
            managementClient.setHost(privateUrl);
            managementClient.requestToken().execute();

            //人员和部门与google比对创建新部门，员工退出google上与authing不匹配的组
            executor.submit(new AuthingCampare(managementClient,service));

            //google部门标记，部门更名
            executor.submit(new DepartmentCompare(managementClient,service));

            //输出GroupMail csv到本地
            //executor.submit(new ListAndSaveAllGroups(managementClient,service));

            //员工加入区域组，员工离职退组
            //executor.submit(new EmloyeeInDB(service,employeeMapper));
        } catch (IOException | GeneralSecurityException e) {
            e.printStackTrace();
        }
    }
}
