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

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.*;
import com.bocloud.ims.interfaces.resource.sangfor.SangforAzService;
import com.bocloud.ims.interfaces.resource.sangfor.SangforHostService;
import com.bocloud.ims.repository.resource.*;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("sangforHostService")
public class SangforHostServiceImpl implements SangforHostService {

    private static Logger logger = LoggerFactory.getLogger(SangforHostServiceImpl.class);

    @Autowired
    private ServerRepository serverRepository;

    @Autowired
    private StorageRepository storageRepository;

    @Autowired
    private SangforIfaceRepository ifaceRepository;
    @Autowired
    private SangforAzRepository sangforAzRepository;
    @Autowired
    private SangforProjectsRepository sangforProjectsRepository;
    @Autowired
    private SangforAzService sangforAzService;

    @Override
    public BocloudResult<GridBean<Server>> list(Pager pager) {
        List<Server> list;
        int total;
        GridBean gridBean;
        try {
            List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
            Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newLinkedHashMap());
            if (sorter.getClass().equals(HashMap.class)) {
                sorter = new LinkedHashMap<>(sorter);
                pager.setSorter(sorter);
            }
            sorter.put("azUuid", Common.ZERO);
            sorter.put("manageIp", Common.ZERO);

            Long vendorId = null;
            for (Param param : params) {
                Map<String, Object> paramMap = param.getParam();
                if (paramMap.containsKey("vendorId")) {
                    vendorId = Long.valueOf(String.valueOf(paramMap.get("vendorId")));
                    paramMap.remove("vendorId");
                }
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();

            total = serverRepository.hostCount(params, vendorId);
            list = serverRepository.hostList(page, rows, params, sorter, vendorId);

            Map<String, String> allAzUuidNameMap = sangforAzService.getAllAzUuidNameMap();
            String azUuid, azName;
            for (Server server : list) {
                azUuid = server.getAzUuid();
                if (StringUtils.isNotEmpty(azUuid)) {
                    azName = allAzUuidNameMap.get(azUuid);
                    if (StringUtils.isNotEmpty(azName)) {
                        server.setAzName(azName);
                    }
                }
            }

            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            return new BocloudResult<>(false, "查询成功");
        }
    }

    @Override
    public BocloudResult<GridBean<Storage>> hostSystemList(Pager pager, RequestContext context) {
        List<Storage> list;
        int total;
        GridBean gridBean;
        try {
            List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
            Map<String, String> sorter = new LinkedHashMap<>();
            pager.setSorter(sorter);

            sorter.put("azId", Common.ZERO);
            sorter.put("name", Common.ZERO);
            sorter.remove("azUuid");

            Long vendorId = null;
            String isSystemDisk = null;
            String type = null;
            String hostGuid = null;
            for (Param param : params) {
                Map<String, Object> paramMap = param.getParam();
                if (paramMap.containsKey("vendorId")) {
                    vendorId = Long.valueOf(String.valueOf(paramMap.get("vendorId")));
                    paramMap.remove("vendorId");
                }
                if (paramMap.containsKey("isSystemDisk")) {
                    isSystemDisk = String.valueOf(paramMap.get("isSystemDisk"));
                    paramMap.remove("isSystemDisk");
                }
                if (paramMap.containsKey("type")) {
                    type = String.valueOf(paramMap.get("type"));
                    paramMap.remove("type");
                }
                if (paramMap.containsKey("hostGuid")) {
                    hostGuid = String.valueOf(paramMap.get("hostGuid"));
                    paramMap.remove("hostGuid");
                }
            }
            if (isSystemDisk.equals("sangforSystem")) {
                type = null;
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            total = storageRepository.syCount(params, vendorId, isSystemDisk, type, hostGuid);
            list = storageRepository.syList(page, rows, params, sorter, vendorId, isSystemDisk, type, hostGuid);

            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new BocloudResult<>(false, e.getMessage());
        }
    }

    @Override
    public BocloudResult<GridBean<SangforIface>> hostIfaceList(Pager pager, RequestContext context) {
        List<SangforIface> list = Lists.newArrayList();
        int total = 0;
        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("id", Common.ONE);
            Long vendorId = null;
            for (Param param : params) {
                Map<String, Object> paramMap = param.getParam();
                if (paramMap.containsKey("vendorId")) {
                    vendorId = Long.valueOf(String.valueOf(paramMap.get("vendorId")));
                    paramMap.remove("vendorId");
                }
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            sorter.put("id", Common.ONE);
            total = ifaceRepository.stCount(params, vendorId);
            list = ifaceRepository.stList(page, rows, params, sorter, vendorId);
//            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult(true, list, "查询成功");
        } catch (Exception e) {
            return new BocloudResult<>(false, "查询成功");
        }
    }

    @Override
    public BocloudResult<GridBean<SangforAz>> listAz(Pager pager, RequestContext context) {
        List<SangforAz> list = Lists.newArrayList();
        int total = 0;
        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("id", Common.ONE);
            Long vendorId = null;
            for (Param param : params) {
                Map<String, Object> paramMap = param.getParam();
                if (paramMap.containsKey("vendorId")) {
                    vendorId = Long.valueOf(String.valueOf(paramMap.get("vendorId")));
                    paramMap.remove("vendorId");
                }
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            if (simple) {
                total = sangforAzRepository.stCount(params, vendorId);
                list = sangforAzRepository.list(page, rows, params, sorter, vendorId);
            } else {
                total = sangforAzRepository.stCount(params, vendorId);
                list = sangforAzRepository.stList(page, rows, params, sorter, vendorId);
            }
            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult(true, gridBean, "查询成功");
        } catch (Exception e) {
            return new BocloudResult<>(false, "查询成功");
        }
    }

    @Override
    public BocloudResult<GridBean<SangforProject>> listUsers(Pager pager, RequestContext context) {
        List<SangforProject> list = Lists.newArrayList();
        int total = 0;
        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("id", Common.ONE);
            Long vendorId = null;
            for (Param param : params) {
                Map<String, Object> paramMap = param.getParam();
                if (paramMap.containsKey("vendorId")) {
                    vendorId = Long.valueOf(String.valueOf(paramMap.get("vendorId")));
                    paramMap.remove("vendorId");
                }
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            sorter.put("id", Common.ONE);
            if (simple) {
                total = sangforProjectsRepository.stCount(params, vendorId);
                list = sangforProjectsRepository.list(page, rows, params, sorter, vendorId);
            } else {
                total = sangforProjectsRepository.stCount(params, vendorId);
                list = sangforProjectsRepository.stList(page, rows, params, sorter, vendorId);
            }
            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult(true, gridBean, "查询成功");
        } catch (Exception e) {
            return new BocloudResult<>(false, "查询成功");
        }
    }
}
