//    This file is part of Motion Watch.

//    Motion Watch is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    Motion Watch is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

#include "mo_detect.h"
#include "obj_detect.h"

void detectLoop(shared_t *share)
{
    vector<string> bufFiles;

    while (!bufFiles.empty() || !share->recLoopWait)
    {
        bufFiles = lsFilesInDir(share->buffDir, "." + share->vidExt);

        if ((bufFiles.size() >= 2) || (share->recLoopWait && !bufFiles.empty()))
        {
            Rect blockArea;
            Mat  blockImg;

            if (moDetect(bufFiles[0], &blockArea, &blockImg, share))
            {
                if (objectInImage(blockImg, blockArea))
                {
                    share->skipCmd = true;

                    wrOut(bufFiles[0], share);
                }
                else
                {
                    remove(bufFiles[0].c_str());
                }
            }
            else
            {
                remove(bufFiles[0].c_str());
            }
        }
        else
        {
            sleep(1);
        }
    }
}

void recLoop(shared_t *share)
{
    while (rdConf(share))
    {
        createDirTree(share->buffDir);

        auto bufPath = cleanDir(share->buffDir) + "/%03d." + share->vidExt;
        auto secs    = to_string(share->secs);
        auto limSecs = to_string(share->secs + 3);
        auto cmd     = "timeout -k 1 " + limSecs + " ffmpeg -hide_banner -i " + share->recordUrl + " -y -vcodec copy -map 0 -segment_time 00:00:10 -f segment -t " + secs + " " + bufPath;

        thread th2(detectLoop, share);

        system(cmd.c_str());

        share->recLoopWait = true;

        th2.join();

        if (!share->skipCmd)
        {
            system(share->postCmd.c_str());
        }
    }
}

int main(int argc, char** argv)
{
    struct shared_t sharedRes;

    sharedRes.conf = parseForParam("-c", argc, argv, false);

    if (parseForParam("-h", argc, argv, true) == "true")
    {
        cout << "Motion Watch " << APP_VER << endl << endl;
        cout << "Usage: mow <argument>" << endl << endl;
        cout << "-h : display usage information about this application." << endl;
        cout << "-c : path to the config file." << endl;
        cout << "-v : display the current version." << endl;
    }
    if (parseForParam("-v", argc, argv, true) == "true")
    {
        cout << APP_VER << endl;
    }
    else if (sharedRes.conf.empty())
    {
        cerr << "err: A config file was not given in -c" << endl;
    }
    else
    {
        sharedRes.retCode     = 0;
        sharedRes.tmpId       = 0;
        sharedRes.recLoopWait = false;
        sharedRes.skipCmd     = false;
        sharedRes.init        = true;

        thread th1(recLoop, &sharedRes);

        th1.join();

        return sharedRes.retCode;
    }

    return EINVAL;
}
