package com.zhongshu.card.server.core.service.devices;

import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.models.project.ProjectSyncParam;
import com.github.microservice.models.type.CommonState;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.type.OrganizationState;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.event.ProjectSyncEvent;
import com.zhongshu.card.server.core.httpRequest.ApiRequestService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author TRX
 * @date 2024/6/27
 */
@Slf4j
@Service
public class ProjectSyncIotCenterService {

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    ApiRequestService apiRequestService;

    public ResultContent noticeSyncProjects(List<Organization> list) {
        if (ObjectUtils.isNotEmpty(list)) {
            List<String> projectOIds = list.stream().map(it -> it.getOid()).collect(Collectors.toList());
            ProjectSyncEvent event = new ProjectSyncEvent(this, projectOIds);
            applicationContext.publishEvent(event);
        }
        return ResultContent.buildSuccess();
    }

    public ResultContent noticeSyncProject(Organization organization) {
        if (ObjectUtils.isNotEmpty(organization) && organization.getAuthType() == AuthType.Project) {
            List<String> projectOIds = List.of(organization.getOid());
            ProjectSyncEvent event = new ProjectSyncEvent(this, projectOIds);
            applicationContext.publishEvent(event);
        }
        return ResultContent.buildSuccess();
    }

    @EventListener(classes = ProjectSyncEvent.class)
    @Async
    @SneakyThrows
    public void syncProjectInfo(ProjectSyncEvent event) {
        List<String> projectOIds = event.getProjectOIds();
        log.info("syncProjectInfo: {}", projectOIds.size());
        if (ObjectUtils.isNotEmpty(projectOIds)) {
            List<Organization> organizations = organizationDao.findByOidIn(projectOIds);
            List<ProjectSyncParam.ProjectSyncInfo> list = organizations.stream().map(it -> {
                ProjectSyncParam.ProjectSyncInfo info = new ProjectSyncParam.ProjectSyncInfo();
                info.setCode(it.getCode());
                info.setName(it.getName());
                OrganizationState state = it.getState();
                if (state == OrganizationState.Normal) {
                    info.setState(CommonState.Enable);
                } else {
                    info.setState(CommonState.Disable);
                }
                return info;
            }).collect(Collectors.toList());
            ProjectSyncParam param = new ProjectSyncParam();
            param.setList(list);
            // 请求iot平台
//            APIResponseModel api = apiRequestService.sendIotCenterAPI(IotCenterAPIConfig.syncFromFullCardProjects, param);
//            if (api.isSuccess()) {
//                log.info("同步项目成功");
//            } else {
//                log.error("同步项目失败：{}", api.getMsg());
//            }
        }
    }
}
