package com.bocloud.ims.service.resource.kubernetes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.kubernetes.KubeContainer;
import com.bocloud.ims.interfaces.resource.kubernetes.KubeContainerService;
import com.bocloud.ims.repository.kubernetes.KubeContainerRepository;
import com.google.common.collect.Maps;

@Service("kubeContainerService")
public class KubeContainerServiceImpl implements KubeContainerService {

	private static final Logger logger = LoggerFactory.getLogger(KubeContainerServiceImpl.class);
	@Autowired
	private KubeContainerRepository kubeContainerRepository;

	@Override
	public BocloudResult list(Pager pager, RequestContext context) {
		GridBean gridBean = null;
		try {
			List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
			Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
			Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
			sorter.put("gmtCreate", Common.ONE);
			Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
			Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);

			int total = 0;
			// 为租户下的用户
			List<KubeContainer> list = null;
			total = this.kubeContainerRepository.count(params);
			if (simple) {
				List<SimpleBean> beans = this.kubeContainerRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				list = this.kubeContainerRepository.list(page, rows, params, sorter);
				gridBean = GridHelper.getBean(page, rows, total, list);
			}
			return new BocloudResult(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("Query pod list fail:", e);
			return new BocloudResult(false, "查询失败");
		}
	}

	@Override
	public BocloudResult create(KubeContainer kubeContainer, Long userId) {
		try {
			return new BocloudResult(true, "开发中...");
		} catch (Exception e) {
			logger.error("create pod error", e);
			return new BocloudResult(false, "添加失败");
		}
	}

	@Override
	public BocloudResult modify(KubeContainer kubeContainer, Long userId) {
		try {
			return new BocloudResult(true, "开发中...");
		} catch (Exception e) {
			logger.error("Modify pod fail:", e);
			return new BocloudResult(false, "修改失败");
		}
	}

	@Override
	public BocloudResult remove(Long id, Long userId) {
		return new BocloudResult(true, "开发中...");
	}

	@Override
	public BocloudResult detail(Long id) {
		try {
			KubeContainer pod = this.kubeContainerRepository.query(id);
			if (null == pod) {
				return new BocloudResult(false, "数据不存在");
			}
			return new BocloudResult(true, pod, "查询详情成功");
		} catch (Exception e) {
			logger.error("Get pod error：", e);
			return new BocloudResult(false, "查询详情失败");
		}
	}

	@Override
	public BocloudResult batchRemove(List<Long> ids, Long userId) {
		return new BocloudResult(true, "开发中...");
	}

}
