#pragma once

#ifdef _WIN32
    #ifdef SNAP7_EXPORTS
        #define S7API __declspec(dllexport)
    #else
        #define S7API __declspec(dllimport)
    #endif
#else
    #define S7API
#endif

#include <snap7.h>

class PLCController {
public:
    PLCController() : client_(nullptr), connected_(false) {
        client_ = new TS7Client();
    }
    
    ~PLCController() {
        if (client_) {
            if (connected_) {
                client_->Disconnect();
            }
            delete client_;
        }
    }
    
    bool connect(const char* ip = "192.168.0.1", int rack = 0, int slot = 1) {
        int result = client_->ConnectTo(ip, rack, slot);
        connected_ = (result == 0);
        return connected_;
    }
    
    bool readDB(int dbNumber, int start, int size, void* buffer) {
        if (!connected_) return false;
        int result = client_->DBRead(dbNumber, start, size, buffer);
        return (result == 0);
    }
    
    bool writeDB(int dbNumber, int start, int size, void* buffer) {
        if (!connected_) return false;
        int result = client_->DBWrite(dbNumber, start, size, buffer);
        return (result == 0);
    }

private:
    TS7Client* client_;
    bool connected_;
}; 