<?php
namespace EdgeManager\EController;

class EConfigure {
    function __construct(
        protected $dbconn,
        protected $post = NULL,
        protected $get = NULL,
    ) {}
    
    function add_server() {
        pg_query($this -> dbconn, "BEGIN");
        $addr_exists = pg_query($this -> dbconn, sprintf(
            "SELECT EXISTS (
                SELECT 1 FROM hf_mes_scada_edgeserver_controller_server
                WHERE url = '%s'
                AND port = '%s'
            )",
            $this -> post -> url,
            $this -> post -> port
        ));
        $name_exists = pg_query($this -> dbconn, sprintf(
            "SELECT EXISTS (
                SELECT 1 FROM hf_mes_scada_edgeserver_controller_server
                WHERE name = '%s'
            )",
            $this -> post -> name
        ));

        if (
            pg_fetch_assoc($addr_exists)['exists'] === 't'
            or pg_fetch_assoc($name_exists)['exists'] === 't'
        ) {
            pg_query($this -> dbconn, "ROLLBACK");
            return "REPLICATED";
        } else {
            $res = pg_insert(
                $this -> dbconn,
                'hf_mes_scada_edgeserver_controller_server',
                (array) $this -> post
            );
            if ($res) {
                pg_query($this -> dbconn, "COMMIT");
                return true;
            } else {
                pg_query($this -> dbconn, "ROLLBACK");
                return false;
            }
        }
    }

    function remove_server() {
        $res = pg_delete(
            $this -> dbconn,
            'hf_mes_scada_edgeserver_controller_server',
            (array) $this -> post,
        );

        return $res ? true : "REMAINING";
    }
    
    function update_server() {
        return pg_update(
            $this -> dbconn,
            'hf_mes_scada_edgeserver_controller_server',
            (array) $this -> post,
            ['id' => $this -> post -> id]
        );
    }

    static function get_servers($dbconn) {
        $res = pg_query($dbconn, "SELECT * FROM hf_mes_scada_edgeserver_controller_server");
        return pg_fetch_all($res);
    }

    function add_device() {
        $res = pg_insert(
            $this -> dbconn,
            'hf_mes_scada_edgeserver_controller_device',
            (array) $this -> post
        );
        
        return $res === false ? $res : true;
    }

    function remove_device() {
        return pg_delete(
            $this -> dbconn,
            'hf_mes_scada_edgeserver_controller_device',
            (array) $this -> post
        );
    }

    function update_device() {
        pg_query($this -> dbconn, "BEGIN");

        $res[] = pg_update(
            $this -> dbconn,
            'hf_mes_scada_edgeserver_controller_device',
            (array) $this -> post,
            ['id' => $this -> post -> id]
        );

        $res[] = $id_res = pg_query(
            $this -> dbconn, sprintf(
                "SELECT server_id
                FROM hf_mes_scada_edgeserver_controller_device
                WHERE id = '%s'
                LIMIT 1",
                $this -> post -> id
            )
        );
        $server_id = pg_fetch_row($id_res)[0];

        $res[] = pg_update(
            $this -> dbconn,
            "hf_mes_scada_edgeserver_controller_server",
            ['updated'  => true],
            ['id'       => $server_id]
        );

        if (in_array(false, $res)) {
            pg_query($this -> dbconn, "ROLLBACK");
            return false;
        } else {
            pg_query($this -> dbconn, "COMMIT");
            return true;
        }
    }

    function get_device() {
        $res = pg_query($this -> dbconn, sprintf(
            "SELECT * FROM hf_mes_scada_edgeserver_controller_device
            WHERE id = '%s'",
            $this -> get['id']
        ));
        return pg_fetch_all($res);
    }

    function get_device_list() {
        $res = pg_query($this -> dbconn, sprintf(
            "SELECT id, name FROM hf_mes_scada_edgeserver_controller_device
            WHERE server_id = '%s'",
            $this -> get['id']
        ));
        return pg_fetch_all($res);
    }
}
