<?php

/**
 *
 */
class ProcessesController{
    private $model;

    function __construct(){
        $this->model = new ProcessesModel();
    }

    /**
     * get info for processes
     * @return json process info
     */
    public function getProcessInfo() {
        echo json_encode($this->model->getProcessInfo());
    }

    /**
     * Get only the processes that match the wanted string
     * @return json list of processes
     */
    public function searchProcess() {
        $nameToSearch = $_REQUEST['name'] ? $_REQUEST['name'] : '';
        echo json_encode($this->model->searchProcess($nameToSearch));
    }

    /**
     * add or update links between process and package
     * @return json update message
     */
    public function linkProcessesToPackage(){
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $selectedProcesses = isset($_REQUEST['selectedProcesses']) ? $_REQUEST['selectedProcesses'] : '';
        echo json_encode($this->model->linkProcessesToPackage($idPackage, $selectedProcesses));
    }

    /**
     * returns json response for packages that don't have processes associated
     * @return list packages json
     */
    public function getPackagesAndProcesses(){
        $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
        echo json_encode($this->model->getPackagesAndProcesses($idCountry));
    }

    /**
     * get all processes for packages linking including the steps for each process
     * @return json list of processes
     */
    public function getProcesses(){
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        echo json_encode($this->model->getProcesses($idPackage));
    }

    /**
     * returns confirmation message after adding process step in DB
     * @return status message for process step creation
     */
    public function addProcessStep() {
        $shortDesc = isset($_REQUEST['shortDesc']) ? $_REQUEST['shortDesc'] : '';
        $fullDesc = isset($_REQUEST['fullDesc']) ? $_REQUEST['fullDesc'] : '';
        $idUserType = isset($_REQUEST['idUserType']) ? $_REQUEST['idUserType'] : 0;
        $isStepVisible = isset($_REQUEST['isStepVisible']) ? $_REQUEST['isStepVisible'] : 1;
        $extraActionCode = isset($_REQUEST['extraActionCode']) ? $_REQUEST['extraActionCode'] : '';
        echo json_encode($this->model->addProcessStep($shortDesc, $fullDesc, $idUserType, $isStepVisible, $extraActionCode));
    }

    /**
     * returns status message after adding process in DB
     * @return status message for process creation
     */
    public function addProcess() {
        $data = isset($_REQUEST['data']) ? $_REQUEST['data'] : [];
        $processName = isset($_REQUEST['processName']) ? $_REQUEST['processName'] : '';
        $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : '';
        echo json_encode($this->model->addProcess(json_decode($data), $processName, $idCountry));
    }

    /**
     * get process names and ids for edit
     * @return json list of processes
     */
    public function getProcessNames(){
        echo json_encode($this->model->getProcessNames());
    }

    /**
     * get all steps and steps for process selected
     * @return json list of steps
     */
    public function getStepsForProcessSelected(){
        $idProcess = isset($_REQUEST['idProcess']) ? $_REQUEST['idProcess'] : 0;
        echo json_encode($this->model->getStepsForProcessSelected($idProcess));
    }

    /**
     * edit processes
     * @return status message
     */
    public function editProcess() {
        $idProcess = isset($_REQUEST['idProcess']) ? $_REQUEST['idProcess'] : 0;
        $processSteps = isset($_REQUEST['data']) ? $_REQUEST['data'] : [];
        echo json_encode($this->model->editProcess($idProcess, $processSteps));
    }

    /**
     * returns json response with default process steps
     * @return array with existing process steps
     */
    public function getProcessSteps() {
        $packageId = isset($_REQUEST['packageId']) ? $_REQUEST['packageId'] : 0;
        echo json_encode($this->model->getProcessSteps($packageId));
    }

    /**
     * get all possible user types
     */
    public function getUserTypes() {
        echo json_encode($this->model->getUserTypes());
    }

    /**
     * get all possible extra actions for process steps
     */
    public function getExtraActionsAvailable() {
        echo json_encode($this->model->getExtraActionsAvailable());
    }

    /**
     * include copy processes template
     */
    public function copyProcessesTemplate(){
        require_once('templates/CopyProcessesTemplate.php');
    }

    /**
     * include edit processes template
     */
    public function editProcessesTemplate(){
        require_once('templates/EditProcessesTemplate.php');
    }

    /**
     * include create process template
     */
    public function createProcessTemplate() {
        require_once('templates/CreateProcessTemplate.php');
    }

    /**
     * include link processes template
     */
    public function linkProcessTemplate(){
        require_once('templates/LinkProcessTemplate.php');
    }

    /**
     * include view processes template
     */
    public function viewPackageProcessesTemplate(){
        require_once('templates/ViewPackageProcessesTemplate.php');
    }

    /**
     * include processes template
     */
    public function processesTemplate(){
        require_once('templates/ProcessesTemplate.php');
    }

    /**
     * open processes page
     */
    public function showPage(){
        require_once('ProcessesPage.php');
    }
}
