#include "3rd/iniparser/iniparser.h"
#include "AppSetting.h"

AppSetting* AppSetting::_instance = new AppSetting();

AppSetting* AppSetting::Instance()
{
    return _instance;
}

bool AppSetting::Load(const std::string& file)
{
    dictionary* ini= iniparser_load(file.c_str());
    if (ini==NULL) {
        return false;
    }


    iniparser_dump(ini, stderr);

    /* Get pizza attributes */

    redisPort = iniparser_getint(ini, "base:redis.port", -1); 
    redisIp = iniparser_getstring(ini, "base:redis.ip", "");
    
    f3Port = iniparser_getint(ini, "base:f3.port", 9033);
    f3Ip = iniparser_getstring(ini, "base:f3.ip", "127.0.0.1");
    f3timeout = iniparser_getint(ini, "base:f3.timeout", 3);
    f3id = iniparser_getstring(ini, "base:f3.id", "streaming_media_client_id");
    kafkaHost = iniparser_getstring(ini, "base:f3.kafkahost", "192.168.24.106:6667,192.168.24.107:6667,192.168.24.108:6667");

    enableKafka = iniparser_getint(ini, "base:f3.enableKafka", 1) == 1;
    enableSocket = iniparser_getint(ini, "base:f3.enableSocket", 1) == 1;
    rtpIpSend = iniparser_getstring(ini, "base:rtp.ip", "");


    childProcessState[0].name = "RealStream";
    childProcessState[0].rtpPort = iniparser_getint(ini, "base:realstream.port.rtp", -1);
    childProcessState[0].rtpPortyb = iniparser_getint(ini, "base:realstream.port.rtpyb", -1);
    childProcessState[0].websocketPort = iniparser_getint(ini, "base:realstream.port.web", -1);
    childProcessState[0].infoPort = iniparser_getint(ini, "base:realstream.port.info", -1);
    childProcessState[0].appPort = iniparser_getint(ini, "base:realstream.port.app", -1);
    childProcessState[0].rtmpPort = iniparser_getint(ini, "base:realstream.port.rtmp", 1935);
    childProcessState[0].rtpPortSend = iniparser_getint(ini, "base:rtp.port.realtime", -1);
    childProcessState[0].rtpPortSendyb = iniparser_getint(ini, "base:rtp.port.realtimeyb", -1);

    childProcessState[1].name = "Talkback";
    childProcessState[1].rtpPort = iniparser_getint(ini, "base:talkback.port.rtp", -1);
    childProcessState[1].rtpPortyb = iniparser_getint(ini, "base:talkback.port.rtpyb", -1);
    childProcessState[1].websocketPort = iniparser_getint(ini, "base:talkback.port.web", -1);
    childProcessState[1].infoPort = iniparser_getint(ini, "base:talkback.port.info", -1);
    childProcessState[1].appPort = iniparser_getint(ini, "base:talkback.port.app", -1);
    childProcessState[1].rtmpPort = iniparser_getint(ini, "base:talkback.port.rtmp", 1935);
    childProcessState[1].rtpPortSend = iniparser_getint(ini, "base:rtp.port.talkback", -1);
    childProcessState[1].rtpPortSendyb = iniparser_getint(ini, "base:rtp.port.talkbackyb", -1);

    childProcessState[2].name = "Playback";
    childProcessState[2].rtpPort = iniparser_getint(ini, "base:playback.port.rtp", -1);
    childProcessState[2].rtpPortyb = iniparser_getint(ini, "base:playback.port.rtpyb", -1);
    childProcessState[2].websocketPort = iniparser_getint(ini, "base:playback.port.web", -1);
    childProcessState[2].infoPort = iniparser_getint(ini, "base:playback.port.info", -1);
    childProcessState[2].appPort = iniparser_getint(ini, "base:playback.port.app", -1);
    childProcessState[2].rtmpPort = iniparser_getint(ini, "base:playback.port.rtmp", 1935);
    childProcessState[2].rtpPortSend = iniparser_getint(ini, "base:rtp.port.playback", -1);
    childProcessState[2].rtpPortSendyb = iniparser_getint(ini, "base:rtp.port.playbackyb", -1);

    holdTalkWeb = iniparser_getboolean(ini, "setting:savetalk.enable", -1);
    holdTalkWebPath = iniparser_getstring(ini, "setting:savetalk.path", "/tmp/zwtalk/");

    logLevel = iniparser_getint(ini, "setting:log.level", 0);
    logMaxTime = iniparser_getint(ini, "setting:log.maxtime", 60);
    logMaxSize = iniparser_getint(ini, "setting:log.maxsize", 100);
    logDir = iniparser_getstring(ini, "setting:log.dir", "log");
    logHoldTime = iniparser_getint(ini, "setting:log.holdtime", 24*7);
    
    streamDir = iniparser_getstring(ini, "setting:stream.dir", "/tmp/zw/data/");

    readbufferMax = iniparser_getint(ini, "setting:buffer.read.max", 1024000);


    EpollThreadCount = iniparser_getint(ini, "setting:thread.count", 0);
    
    playVersion = iniparser_getint(ini, "setting:version.play", 1);
    ZeroCopyEnabled = iniparser_getint(ini, "setting:version.0copy", 0) > 0;

    TestFileClientWriteEnable = iniparser_getboolean(ini, "setting:test.TestFileClientWriteEnable", false);

    iniparser_freedict(ini);
    return true;
}