<?php
require_once 'dao.php';

class ZapetteHandler
{
    private $valid_actions = ["scan", "validate",];

    private $action;
    private $data;
    private $given_password;
    private $dao;
    private $password;

    private $responseArray = array(
        "status" => 0,
        "message" => "Success",
        "data" => "",
    );

    public function __construct($post)
    {
        $this->read_password();
        $this->given_password = $this->get_password($post);
        $this->action = $this->get_action($post);
        $this->data = $this->get_data($post);
        $this->dao = new Dao();
    }

    public function do_action()
    {
        $result = "";
        if ($this->is_password_valid()) {
            if ($this->action == "scan") {
                $result = $this->get_scanned_article();
                if (sizeof($result) == 0)
                    $this->setUnknownCodeErrorResponse();
            } else if ($this->action == "validate") {
                $result = $this->update_stock();
                if (!$result)
                    $this->setUpdateStockErrorResponse();
            }
        } else {
            $this->setWrongPasswordErrorResponse();
        }


        $this->responseArray["data"] = $result;
        return $this->responseArray;
    }

    private function read_password() {
        $real_path = __DIR__ . "/.htpasszapette";
        $fp = fopen($real_path, 'r');
        $this->password = trim(fread($fp, filesize($real_path)));
        fclose($fp);
    }

    private function is_password_valid() {
        return $this->given_password == $this->password;
    }

    private function get_scanned_article() {
        $article = [];
        if ($this->data != "") {
            $article = $this->dao->get_article_of_code($this->data);
        }
        return $article;
    }

    private function update_stock() {
        $result = false;
        if ($this->data != "") {
            foreach ($this->data as $row) {
                $result = $this->dao->update_article_stock($row["id"], $row["quantity"]);
            }
        }
        return $result;
    }

    function setWrongPasswordErrorResponse()
    {
        $this->responseArray["status"] = 1;
        $this->responseArray["message"] = "Error: Wrong password";
    }

    function setUnknownCodeErrorResponse()
    {
        $this->responseArray["status"] = 2;
        $this->responseArray["message"] = "Error: Unknown code scanned";
    }

    function setUpdateStockErrorResponse()
    {
        $this->responseArray["status"] = 3;
        $this->responseArray["message"] = "Error: Impossible to update stock";
    }

    private function get_action($inputData)
    {
        if (!in_array($inputData["action"], $this->valid_actions))
            return "";
        else
            return $inputData["action"];
    }

    private function get_data($inputData)
    {
        if ($inputData["data"] == null)
            return "";
        else
            return $inputData["data"];
    }

    private function get_password($inputData)
    {
        if ($inputData["password"] == null)
            return "";
        else
            return $inputData["password"];
    }
}