package com.yihu.hos.monitor.service;

import com.mongodb.*;
import com.yihu.hos.monitor.dao.ServiceMonitorDao;
import com.yihu.hos.system.model.SystemServiceFlow;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.model.TreeView;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenweida on 2016/1/27.
 */
@Service
public class ServiceMonitorService {
    public static final String BEAN_ID = "ServiceMonitorService";
    public static final String dbName = "log";
    public static final String tableName = "serviceMetrics";
    public static final String configuration = "configuration";

    @Autowired
    private MongoClient mongoClient;

    @Autowired
    private ServiceMonitorDao serviceMonitorDao;

    public Result serviceMetrics(String tenant, String routeId, String beginTime, String endTime) throws Exception {
        MongoTemplate mongoOperations = new MongoTemplate(mongoClient, dbName);
        DBCollection metrics = mongoOperations.getCollection(tableName);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("time", new BasicDBObject().append(QueryOperators.GTE, beginTime)),
                        new BasicDBObject().append("time", new BasicDBObject().append(QueryOperators.LT, endTime))
//                        new BasicDBObject().append("time", new BasicDBObject().append(QueryOperators.GTE, "201706120000")),
//                        new BasicDBObject().append("time", new BasicDBObject().append(QueryOperators.LT, "201706200000"))

                        , new BasicDBObject("routeId", routeId)
                        , new BasicDBObject("tenant", tenant)
                });

        JSONObject result = new JSONObject();
        DBCursor cursor = metrics.find(queryObject);
        JSONArray data = new JSONArray();
        while (cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            dbObject.removeField("_id");
            data.put(dbObject);
        }

        result.put("data", data);

        return Result.success(result.toString());
    }

    public Result getServiceTreeList() throws Exception {
        List<TreeView> treeList = new ArrayList<>();

        List<SystemServiceFlow> flowList = serviceMonitorDao.getFlowsByType(ServiceFlowConstant.CLASS);

        for (SystemServiceFlow flow : flowList) {
            TreeView rootTree = new TreeView();
            rootTree.setIschecked(false);
            rootTree.setId("flow" + flow.getId());
            rootTree.setPid("-1");
            rootTree.setText(flow.getName());
            treeList.add(rootTree);
        }

        JSONArray jsonArray = new JSONArray(treeList);
        return Result.success(jsonArray.toString());
    }

    public boolean serviceStatus(String routeCode) {
        boolean succ = false;
//        MongoDatabase db = mongoConfig.mongoClient().getDatabase(configuration);
//        MongoCollection<Document> collection = db.getCollection(serviceFlow);

        return succ;
    }

}
