/**
 * Copyright © 2017 IBM Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <memory>
#include "argument.hpp"
#include "evdevpp/evdev.hpp"
#include "sdevent/event.hpp"
#include "sdevent/io.hpp"
#include "utility.hpp"

namespace phosphor
{
namespace fan
{
namespace util
{

ArgumentParser::ArgumentParser(int argc, char** argv)
{
    auto option = 0;
    while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
    {
        if ((option == '?') || (option == 'h'))
        {
            usage(argv);
            exit(1);
        }

        auto i = &options[0];
        while ((i->val != option) && (i->val != 0))
        {
            ++i;
        }

        if (i->val)
        {
            arguments[i->name] = (i->has_arg ? optarg : true_string);
        }
    }
}

const std::string& ArgumentParser::operator[](const std::string& opt)
{
    auto i = arguments.find(opt);
    if (i == arguments.end())
    {
        return empty_string;
    }
    else
    {
        return i->second;
    }
}

void ArgumentParser::usage(char** argv)
{
    std::cerr << "Usage: " << argv[0] << " [options]\n";
    std::cerr << "Options:\n";
    std::cerr << "    --path               evdev devpath\n";
    std::cerr << "    --type               evdev type\n";
    std::cerr << "    --code               evdev code\n";
    std::cerr << std::flush;
}

const option ArgumentParser::options[] =
{
    { "path",   required_argument,  NULL,   'p' },
    { "type",   required_argument,  NULL,   't' },
    { "code",   required_argument,  NULL,   'c' },
    { 0, 0, 0, 0},
};

const char* ArgumentParser::optionstr = "p:t:c:";

const std::string ArgumentParser::true_string = "true";
const std::string ArgumentParser::empty_string = "";

static void exit_with_error(const char* err, char** argv)
{
    ArgumentParser::usage(argv);
    std::cerr << "\n";
    std::cerr << "ERROR: " << err << "\n";
    exit(1);
}

} // namespace util
} // namespace fan
} // namespace phosphor

int main(int argc, char* argv[])
{
    using namespace phosphor::fan::util;

    auto options = std::make_unique<ArgumentParser>(argc, argv);
    auto path = (*options)["path"];
    auto stype = (*options)["type"];
    auto scode = (*options)["code"];
    unsigned int type = EV_KEY;

    if (path == ArgumentParser::empty_string)
    {
        exit_with_error("Path not specified or invalid.", argv);
    }
    if (stype != ArgumentParser::empty_string)
    {
        type = stoul(stype);
    }

    if (scode == ArgumentParser::empty_string)
    {
        exit_with_error("Keycode not specified or invalid.", argv);
    }
    options.reset();

    auto loop = sdevent::event::newDefault();
    phosphor::fan::util::FileDescriptor fd(
            open(path.c_str(), O_RDONLY | O_NONBLOCK));
    auto evdev = evdevpp::evdev::newFromFD(fd());
    sdevent::event::io::IO callback(
            loop,
            fd(),
            [&evdev](auto& s)
            {
                unsigned int type, code, value;
                std::tie(type, code, value) = evdev.next();
                std::cout <<
                    "type: " << libevdev_event_type_get_name(type) <<
                    " code: " << libevdev_event_code_get_name(type, code) <<
                    " value: " << value << "\n";
            });

    auto value = evdev.fetch(type, stoul(scode));
    std::cout <<
        "type: " << libevdev_event_type_get_name(type) <<
        " code: " << libevdev_event_code_get_name(type, stoul(scode)) <<
        " value: " << value << "\n";

    loop.loop();

    return 0;
}
