package com.ruoyi.platform.service.impl;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.platform.domain.PodStatus;
import com.ruoyi.platform.service.TensorBoardService;
import com.ruoyi.platform.utils.K8sClientUtil;
import com.ruoyi.platform.vo.FrameLogPathVo;
import com.ruoyi.platform.vo.PodStatusVo;
import com.ruoyi.system.api.constant.Constant;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TensorBoardServiceImpl implements TensorBoardService {
    @Value("${tensorBoard.image}")
    private String image;
    @Value("${tensorBoard.port}")
    private Integer port;
    @Value("${tensorBoard.mountPath}")
    private String mountPath;
    @Value("${tensorBoard.masterIp}")
    private String masterIp;
    @Resource
    private RedisService redisService;
    @Resource
    private K8sClientUtil k8sClientUtil;

    @Override
    public PodStatusVo getTensorBoardStatus(FrameLogPathVo frameLogPathVo) {
        String status = PodStatus.Terminated.getName();
        PodStatusVo tensorboardStatusVo = new PodStatusVo();
        tensorboardStatusVo.setStatus(status);
        if (StringUtils.isEmpty(frameLogPathVo.getPath())) {
            return tensorboardStatusVo;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String podName = loginUser.getUsername().toLowerCase() + "-" + frameLogPathVo.getPath().split("/")[1] + "-tensorboard-pod";

        try {
            String podStatus = k8sClientUtil.getPodStatus(podName, StringUtils.isEmpty(frameLogPathVo.getNamespace()) ? "default" : frameLogPathVo.getNamespace());
            for (PodStatus s : PodStatus.values()) {
                if (s.getName().equals(podStatus)) {
                    status = s.getName();
                    break;
                }
            }
        } catch (Exception e) {
            return tensorboardStatusVo;
        }
        String url = redisService.getCacheObject(podName);
        tensorboardStatusVo.setStatus(status);
        tensorboardStatusVo.setUrl(url);
        return tensorboardStatusVo;
    }

    @Override
    public String runTensorBoard(FrameLogPathVo frameLogPathVo) throws Exception {
//        if (StringUtils.isEmpty(frameLogPathVo.getPath())||StringUtils.isEmpty(frameLogPathVo.getPvcName())){
//            throw new Exception("存储路径或存储为空");
//        }
        if (StringUtils.isEmpty(frameLogPathVo.getPath())) {
            throw new Exception("存储路径为空");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String podName = loginUser.getUsername().toLowerCase() + "-" + frameLogPathVo.getPath().split("/")[1] + "-tensorboard-pod";

//        Integer podPort = k8sClientUtil.createPodWithSubPath(podName, StringUtils.isEmpty(frameLogPathVo.getNamespace()) ? "default" : frameLogPathVo.getNamespace(), port, mountPath, frameLogPathVo.getPath(), frameLogPathVo.getPvcName(), image);
        Integer podPort = k8sClientUtil.createPodWithSubPath(podName, StringUtils.isEmpty(frameLogPathVo.getNamespace()) ? "default" : frameLogPathVo.getNamespace(), port, mountPath, frameLogPathVo.getPath(), image);
        redisService.setCacheObject(podName, masterIp + ":" + podPort);
        return masterIp + ":" + podPort;
    }
}
