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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.monitor.MonitorDriver;
import com.bocloud.cmp.model.fusionsphere.FusionSphereMonitorRealTimeModel;
import com.bocloud.common.model.*;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.entity.resource.Vdc;
import com.bocloud.ims.entity.resource.fusionsphere.FusionSphereUpLink;
import com.bocloud.ims.interfaces.resource.fusionsphere.FusionSphereUpLinkService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.ServerRepository;
import com.bocloud.ims.repository.resource.VdcRepository;
import com.bocloud.ims.repository.resource.fusionsphere.FusionSphereUpLinkRepository;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: FusionSphereUpLinkServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/11/3 13:50
 */
@Service
@Slf4j
public class FusionSphereUpLinkServiceImpl implements FusionSphereUpLinkService {

    @Autowired
    private FusionSphereUpLinkRepository upLinkRepository;
    @Autowired
    private MonitorDriver monitorDriver;
    @Autowired
    private ServerRepository serverRepository;
    @Autowired
    private CloudVendorRepository vendorRepository;
    @Autowired
    private VdcRepository vdcRepository;

    @Override
    public BocloudResult<GridBean<FusionSphereUpLink>> list(Pager pager, RequestContext context) {
        List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
        Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
        Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
        Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
        try {
            int total = upLinkRepository.count(params);
            List<FusionSphereUpLink> list = this.upLinkRepository.list(page, rows, params, sorter);

            for(FusionSphereUpLink upLink:list){
             //补全4个网口速率
                Server server = serverRepository.query(upLink.getHostId());
                CloudVendor vendor = vendorRepository.query(upLink.getVendorId());
                if (ObjectUtils.isEmpty(vendor)) {
                    log.error("get cloudVendor failure");
                    continue;
                }
                // 站点
                Vdc vdc = vdcRepository.query(server.getVdcId());
                if (ObjectUtils.isEmpty(vdc)) {
                    log.error("get vdc failure");
                    continue;
                }

                FusionSphereMonitorRealTimeModel monitorModel2 = new FusionSphereMonitorRealTimeModel();
                monitorModel2.setSiteUri(vdc.getUri());
                monitorModel2.setUrn(server.getUuid());
                List<String> metricIds = new ArrayList<>();
                metricIds.add("net_dynamic_info");
                monitorModel2.setMetricId(metricIds);
                BocloudResult result = monitorDriver.metrics(vendor.getUuid(),monitorModel2,vdc.getUri());
                if(result.isFailed()) {
                    continue;
                }
                String rs = (String) result.getData();
                JSONObject resultjsonobject = JSONObject.parseObject(rs);
                JSONArray items = resultjsonobject.getJSONArray("items");
                JSONObject item = items.getJSONObject(0);
                JSONArray values = item.getJSONArray("value");
                JSONObject value = values.getJSONObject(0);
                JSONArray metricValues = value.getJSONArray("metricValue");
                JSONObject metricvalue = (JSONObject) metricValues.get(0);
                for(int i=0;i<metricValues.size();i++){
                    JSONObject jsonObject = (JSONObject) metricValues.get(i);
                    if (upLink.getPortUuid().contains(jsonObject.getString("urn"))){
                        metricvalue = jsonObject;
                    }
                }
                upLink.setByteOut(metricvalue.getString("byte_out"));
                upLink.setByteIn(metricvalue.getString("byte_in"));
                upLink.setPkgSend(metricvalue.getString("pkg_send"));
                upLink.setPkgRcv(metricvalue.getString("pkg_rcv"));
            }

            GridBean<FusionSphereUpLink> gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            log.error("list UpLink error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }
    }
}