package cd.casic.ci.process.process.service.resource.impl;

import cd.casic.ci.process.constant.ResourceConstant;
import cd.casic.ci.process.dto.req.resource.ResourceCreateReq;
import cd.casic.ci.process.dto.req.resource.ResourceListReq;
import cd.casic.ci.process.dto.req.resource.ResourcePageReq;
import cd.casic.ci.process.dto.req.resource.ResourceUpdateReq;
import cd.casic.ci.process.dto.resp.resource.ResourceDetailResp;
import cd.casic.ci.process.dto.resp.resource.ResourceListResp;
import cd.casic.ci.process.process.converter.ResourceConverter;
import cd.casic.ci.process.process.dao.pipeline.PipResourceManagerDao;
import cd.casic.ci.process.process.dataObject.resource.PipResourceManager;
import cd.casic.ci.process.process.service.resource.ResourceManagerService;
import cd.casic.framework.commons.pojo.PageResult;
import cd.casic.module.execute.docker.dao.DockerEndpointDao;
import cd.casic.module.execute.docker.dataobject.dto.DockerEndpointDo;
import cd.casic.module.execute.docker.dataobject.model.DockerEndpoint;
import cd.casic.module.machine.dal.dataobject.MachineInfoDO;
import cd.casic.module.machine.service.MachineInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.resource.ResourceManager;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author HopeLi
 * @version v1.0
 * @ClassName ResourceServiceImpl
 * @Date: 2025/5/13 10:31
 * @Description:
 */
@Service
@Slf4j
public class ResourceManagerServiceImpl extends ServiceImpl<PipResourceManagerDao, PipResourceManager> implements ResourceManagerService {
    @Resource
    private PipResourceManagerDao pipResourceManagerDao;
    @Resource
    private DockerEndpointDao dockerEndpointDao;
    @Resource
    private MachineInfoService machineInfoService;
    @Resource
    private ResourceConverter converter;

    @Override
    public String createResource(ResourceCreateReq req) {
        String resourceType = req.getResourceType();
        PipResourceManager manager = new PipResourceManager();
        manager.setResourceName(req.getResourceName());
        if (ResourceConstant.RES_TYPE_CUSTOM.equals(resourceType)) {
            manager.setResourceType(ResourceConstant.RES_TYPE_CUSTOM);
            Long machineInfoId = req.getMachineInfoId();
            MachineInfoDO machineInfoDO = machineInfoService.validateMachineInfoExists(machineInfoId);
            manager.setMachineId(machineInfoDO.getId());
            // 判断docker是否需要新增docker记录
            if (Objects.nonNull(req.getDockerPort())) {
                DockerEndpointDo dockerEndpoint = new DockerEndpointDo();
                dockerEndpoint.setName(machineInfoDO.getName());
                dockerEndpoint.setPort(req.getDockerPort());
                dockerEndpoint.setHost(machineInfoDO.getHostIp());
                dockerEndpoint.setType(DockerEndpoint.DockerEndpointTypeEnum.REMOTE);
                dockerEndpointDao.insert(dockerEndpoint);
                manager.setDockerId(dockerEndpoint.getId());
            }
            pipResourceManagerDao.insert(manager);
        } else if(ResourceConstant.RES_TYPE_CLOUD.equals(resourceType)){
            // 入库。申请资源等操作
        }
        return manager.getId();
    }

    @Override
    public String updateResource(ResourceUpdateReq req) {
        String resourceType = req.getResourceType();
        String id = req.getId();
        PipResourceManager manager = pipResourceManagerDao.selectById(id);
        manager.setResourceName(req.getResourceName());
        manager.setResourceType(req.getResourceType());
        // 同类型资源修改
        if (manager.getResourceType().equals(resourceType)) {
            if (ResourceConstant.RES_TYPE_CUSTOM.equals(resourceType)) {
                manager.setResourceType(ResourceConstant.RES_TYPE_CUSTOM);
                Long machineInfoId = req.getMachineInfoId();
                MachineInfoDO machineInfoDO = machineInfoService.validateMachineInfoExists(machineInfoId);
                manager.setMachineId(machineInfoDO.getId());
                // 判断docker是否需要新增docker记录,
                // 1.之前没有维护docker信息，之后维护了
                // 2.维护docker信息，之后需要修改
                // 3.之前维护了docker信息，之后不想维护了
                DockerEndpointDo dockerEndpoint = new DockerEndpointDo();
                String dockerId = manager.getDockerId();
                if (StringUtils.isNotEmpty(dockerId)) {
                    dockerEndpoint = dockerEndpointDao.selectById(dockerId);
                }
                if (dockerEndpoint!=null) {
                    dockerEndpoint.setName(machineInfoDO.getName());
                    dockerEndpoint.setPort(req.getDockerPort());
                    dockerEndpoint.setHost(machineInfoDO.getHostIp());
                    dockerEndpoint.setType(DockerEndpoint.DockerEndpointTypeEnum.REMOTE);
                }
                if (StringUtils.isEmpty(dockerId) &&Objects.nonNull(req.getDockerPort())) {
                    dockerEndpointDao.insert(dockerEndpoint);
                    manager.setDockerId(dockerEndpoint.getId());
                } else if (!StringUtils.isEmpty(dockerId) &&Objects.nonNull(req.getDockerPort())) {
                    dockerEndpointDao.updateById(dockerEndpoint);
                    manager.setDockerId(dockerEndpoint.getId());
                } else if (!StringUtils.isEmpty(dockerId) &&!Objects.nonNull(req.getDockerPort())) {
                    dockerEndpointDao.deleteById(dockerId);
                    manager.setDockerId(null);
                }
            } else if(ResourceConstant.RES_TYPE_CLOUD.equals(resourceType)){
                // 入库。申请资源等操作
            }
        } else {
            //天河国云资源转机器资源是否需要销毁原本资源？还是说可以自由切换预留入口
        }
        pipResourceManagerDao.updateById(manager);
        return req.getId();
    }

    @Override
    public ResourceDetailResp findResourceDetailById(String id) {
        PipResourceManager manager = pipResourceManagerDao.selectById(id);
        Long machineId = manager.getMachineId();
        String dockerId = manager.getDockerId();
        MachineInfoDO machineInfoDO = machineInfoService.validateMachineInfoExists(machineId);
        DockerEndpointDo dockerEndpointDo = dockerEndpointDao.selectById(dockerId);
        ResourceDetailResp resp = converter.converter(manager);
        resp.setMachineInfo(machineInfoDO);
        resp.setDockerInfo(dockerEndpointDo);
        return resp;
    }

    @Override
    public void deleteResource(String resourceId) {
        if (StringUtils.isEmpty(resourceId)) {
            return;
        }
        pipResourceManagerDao.deleteById(resourceId);
    }

    @Override
    public PageResult<ResourceListResp> page(ResourcePageReq req) {
        LambdaQueryWrapper<PipResourceManager> wrapper = new LambdaQueryWrapper<>();
        Page<PipResourceManager> page = pipResourceManagerDao.selectPage(new Page<>(req.getPageNo(), req.getPageSize()), wrapper);
        List<ResourceListResp> list  = converter.converter(page.getRecords());
        return new PageResult<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }

    @Override
    public List<ResourceListResp> list(ResourceListReq req) {
        LambdaQueryWrapper<PipResourceManager> wrapper = new LambdaQueryWrapper<>();
        List<PipResourceManager> list = pipResourceManagerDao.selectList(wrapper);
        return converter.converter(list);
    }
}
