#include "AmfToDataApiImpl.h"
#include <nlohmann/json.hpp>
#include "AMFApiServer.hpp"

#include "amf_statistics.hpp"
#include <string>  
#include <iostream>  

using namespace std;  

extern statistics stacs;

namespace oai {
namespace amf {
namespace api {
AmfToDataApiImpl::AmfToDataApiImpl(
    std::shared_ptr<Pistache::Rest::Router> rtr,
    amf_application::amf_app* amf_app_inst)
    : AmfToDataApi(rtr), m_amf_app(amf_app_inst) {}
//获取gnb列表
void AmfToDataApiImpl::gnbs_get_am_data(
        Pistache::Http::ResponseWriter &response
    ) {
        response.headers().add<Pistache::Http::Header::AccessControlAllowOrigin>("*");
        nlohmann::json response_data_json = {};
        nlohmann::json json_array = nlohmann::json::array();
       
        if (stacs.gnbs.size() > 0) {
            for (int i = 0; i < stacs.gnbs.size(); i++) {
               
               json_array[i]["gnb_name"] = stacs.gnbs[i].gnb_name.c_str();
               /*
               json_array[i]["gnb_plmn"] = (stacs.gnbs[i].mcc + stacs.gnbs[i].mnc).c_str();
               json_array[i]["gnb_tac"] = stacs.gnbs[i].tac;
               */
               json_array[i]["gnb_msg"] = stacs.gnbs[i].plmn_to_string().c_str();
               //json_array[i]["gnb_struct"] = std::to_string(stacs.gnbs[i]).c_str();
               if(GNBS_NUM==i+1){
                   break;
               }

            };
            response_data_json["code"] = 200;
            response_data_json["msg"]= "success"; 
            response_data_json["data"]= json_array;
        }else{
            response_data_json["code"] = 201;
            response_data_json["msg"]= "没有此数据";
            response_data_json["data"]= NULL;
        }
        try {
            response.send(Pistache::Http::Code::Ok, response_data_json.dump()); 
            return;
        } catch (nlohmann::json::exception &e) {
            response.send(Pistache::Http::Code::Not_Found, response_data_json.dump());
            return;
        }
}

//获取ues列表
void AmfToDataApiImpl::ues_get_am_data(
        Pistache::Http::ResponseWriter &response
    ) {
        response.headers().add<Pistache::Http::Header::AccessControlAllowOrigin>("*");
        nlohmann::json response_data_json = {};
        nlohmann::json json_array = nlohmann::json::array();
        //Logger::amf_server().info("=====impl========ues==========");
        if (stacs.ue_infos.size() > 0) {
            response_data_json["code"] = 200;
            response_data_json["msg"]= "success";
            //ues
            int j = 0;
            for (auto const& ue : stacs.ue_infos) {
                //原先
                //  Logger::amf_server().info(
                //      "-|%7d|%22s|%18s|%15s|%16d|%11d|%9s|%7d|", j + 1,
                //      ue.second.registerStatus.c_str(), ue.second.imsi.c_str(),
                //      ue.second.guti.c_str(), ue.second.ranid, ue.second.amfid,
                //      (ue.second.mcc + ue.second.mnc).c_str(), ue.second.cellId);
                // j++;
                //原先
                json_array[j]["ue_Imsi"] = ue.second.imsi.c_str();
                json_array[j]["ue_5GMM-state"] = ue.second.registerStatus.c_str();
                j++;
                if(GNBS_NUM==j){
                   break;
               }
            }
            response_data_json["code"] = 200;
            response_data_json["msg"]= "success";
            response_data_json["data"]= json_array;
        }else{
            response_data_json["code"] = 201;
            response_data_json["msg"]= "没有此数据";
            response_data_json["data"]= NULL;
        }
        try {
            response.send(Pistache::Http::Code::Ok, response_data_json.dump()); 
            return;
        } catch (nlohmann::json::exception &e) {
            response.send(Pistache::Http::Code::Not_Found, response_data_json.dump());
            return;
        }

}

}  // namespace api
}  // namespace amf
}  // namespace oai

