/*
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#ifndef TANGO_WINDOWS_NTSERVICE_H
#define TANGO_WINDOWS_NTSERVICE_H

#include <tango/windows/NTEventLogger.h>

// clang-format off
#include <winsock2.h> // prevents windows.h from including winsock.h
#include <windows.h>
// clang-format on

#include <string>

namespace Tango
{

//
// This class represents an NT service.
//

class NTService
{
  private:
    static NTService *instance_; // The one and only instance

    std::string full_exec_name_;         // Full executable name
    std::string exec_name_;              // Executable name
    std::string name_;                   // The name
    std::string title_;                  // The title
    bool debug_;                         // Are we debugging?
    DWORD checkPoint_;                   // Check point value
    SERVICE_STATUS status_;              // Status of the service
    SERVICE_STATUS_HANDLE statusHandle_; // Status handle

    virtual void start(int, char **, Tango::NTEventLogger *ptr) = 0;

    void control(DWORD);
    void main(int argc, char **argv);

    friend void WINAPI _OB_serviceCtrl(DWORD);
    friend void WINAPI _OB_serviceMain(DWORD, LPTSTR *);
    friend class Util;

  protected:
    void statusUpdate(DWORD, DWORD = NO_ERROR, DWORD = 0);
    bool stopped_;

  public:
    Tango::NTEventLogger *logger_; // The service logger

    bool is_installed();

    NTService(const char *name);

    virtual ~NTService();

    static NTService *instance();

    bool install(char *, bool = false);
    bool uninstall(char *);

    void run(int, char **);
    virtual void stop();

    int options(int, char **);
    void usage(const char *);

    void setDebug()
    {
        debug_ = true;
    }

    bool getDebug() const
    {
        return debug_;
    }
};

} // namespace Tango

#endif
