<?php

  //================================================================================================

  class Application {

    private $sectionCollection = [];
    private $defaultSectionId = '';

    //================================================================================================
    // Function Constructor()
    //================================================================================================

    public function __construct() {
      
      // Set the application sections
      $this->setApplicationSections();
      
      // Set the default section
      $this->defaultSectionId = 'enum';
    }

    //================================================================================================
    // Function setApplicationSections()
    //================================================================================================

    public function setApplicationSections() {
      
      // Set the application section collection that defines the application sections, their id,
      // type and title
      $this->sectionCollection = [
        [ "sectionId"=>'enum', "sectionType"=>'Enum', "sectionTitle"=>'Enumerated Types' ],
        [ "sectionId"=>'enumItem', "sectionType"=>'EnumItem', "sectionTitle"=>'Enum Items' ],
        [ "sectionId"=>'contact', "sectionType"=>'Contact', "sectionTitle"=>'Contacts' ],
        [ "sectionId"=>'contactMethod', "sectionType"=>'ContactMethod', "sectionTitle"=>'Contact Methods' ],
        [ "sectionId"=>'address', "sectionType"=>'Address', "sectionTitle"=>'Addresses' ],
        [ "sectionId"=>'financeCompany', "sectionType"=>'FinanceCompany', "sectionTitle"=>'Finance Companies' ],
        [ "sectionId"=>'product', "sectionType"=>'Product', "sectionTitle"=>'Products' ],
        [ "sectionId"=>'productList', "sectionType"=>'ProductList', "sectionTitle"=>'Product Lists' ],
        [ "sectionId"=>'quote', "sectionType"=>'Quote', "sectionTitle"=>'Quotes' ]
      ];
    }

    //================================================================================================
    // Function processUserRequest()
    //================================================================================================

    public function processUserRequest($requestData) {
      
      $jsonResponseData = '';

      // Process requested action
      switch($requestData["action"]) {
        
        // Create json application data from json navigation data and json section data
        case 'createApplication': $jsonResponseData = $this->processCreateApplicationRequest($requestData); break;
        case 'createSection': $jsonResponseData = $this->processCreateSectionRequest($requestData); break;
        case 'updateApplicationSection': $jsonResponseData = $this->processUpdateApplicationSectionRequest($requestData); break;
        case 'updateTable':
        case 'updateTableBody': $jsonResponseData = $this->processUpdateTableRequest($requestData); break;
        case 'newRecord':
        case 'editRecord': $jsonResponseData = $this->processUpdateRecordRequest($requestData); break;
        case 'insertRecord': $jsonResponseData = $this->processSqlInsertRecordRequest($requestData); break;
        case 'updateRecord': $jsonResponseData = $this->processSqlUpdateRecordRequest($requestData); break;
        case 'closeRecord': $jsonResponseData = $this->processCloseRecordRequest($requestData); break;
        default: break; 
      }

      // Create json response data and respond 
      $this->sendResponse($requestData, $jsonResponseData);
    }

    //================================================================================================
    // Function processCreateApplicationRequest()
    //================================================================================================

    public function processCreateApplicationRequest($requestData) {

      global $navigationJSON;

      $jsonResponseData = '';
      
      $sectionData = [];
      foreach ($this->sectionCollection as $section) {
        if ($section['sectionId'] === $requestData["section"]["sectionId"]) {
          $className = sprintf("%sSection", $section['sectionType']);
          $currentSection = new $className($section);
          $sectionData[] = $currentSection->createJsonSectionData();
        }
      }

      $jsonSectionData = implode(", ", $sectionData);
      $jsonSectionData = sprintf("\"sectionCollection\":[%s]", $jsonSectionData);
      $jsonSectionData .= sprintf(", \"sectionId\": \"%s\"", $requestData["section"]["sectionId"]);
      $jsonResponseData = sprintf("%s,%s", $navigationJSON, $jsonSectionData );

      return $jsonResponseData;
    }

    //================================================================================================
    // Function processUpdateApplicationSectionRequest()
    //================================================================================================

    public function processUpdateApplicationSectionRequest($requestData) {
      
      $jsonResponseData = '';

      $className = sprintf("%sSection", $requestData["section"]["sectionType"]);
      $section = new $className($requestData["section"]);

      // Clear unused section attributes 
      $section->section["sectionTitle"] = "";
      $section->section["sectionType"] = "";
      $section->section["sectionFilter"] = [];
      $section->section["sectionTable"]["sectionTableColumn"] = [];
      $section->section["sectionTable"]["sectionTableMenuButton"] = [];
      $section->section["sectionRecord"] = [];

      $jsonSectionData = $section->createJsonSectionData();
      $jsonResponseData = sprintf("\"section\":%s", $jsonSectionData);

      return $jsonResponseData;
    }

    //================================================================================================
    // Function processUpdateTableRequest()
    //================================================================================================

    public function processCreateSectionRequest($requestData) {
      
      $jsonResponseData = '';

      return $jsonResponseData;
    }

    //================================================================================================
    // Function processUpdateTableRequest()
    //================================================================================================

    public function processUpdateTableRequest($requestData) {
      
      $jsonResponseData = '';

      $className = sprintf("%sSection", $requestData["section"]["sectionType"]);
      $section = new $className($requestData["section"]);

      // Set the section's sql select filters and select statement according to the filter settings
      $section->set_sqlSelectFilters($requestData["filters"]);

      // Update the section's table recordset
      $section->set_sectionTableRecordset();
      
      // Clear unused section attributes 
      $section->section["sectionTitle"] = "";
      $section->section["sectionType"] = "";
      $section->section["sectionFilter"] = [];
      $section->section["sectionTable"]["sectionTableColumn"] = [];
      $section->section["sectionTable"]["sectionTableMenuButton"] = [];
      $section->section["sectionRecord"] = [];

      // Create the json section data
      $jsonSectionData = $section->createJsonSectionData();
      $jsonResponseData = sprintf("\"section\":%s", $jsonSectionData);

      return $jsonResponseData;
    }

    //================================================================================================
    // Function processUpdateRecordRequest()
    //================================================================================================

    public function processUpdateRecordRequest($requestData) {
      
      $jsonResponseData = '';

      $className = sprintf("%sSection", $requestData["section"]["sectionType"]);
      $section = new $className($requestData["section"]);
      
      // Clear unused section attributes 
      $section->section["sectionFilter"] = [];
      $section->section["sectionTable"] = [];

      // If a record is being updated then update the section's record recordset
      $recordSelectedKey = sprintf("%sSelected", $requestData["section"]["sectionId"]);
      if ( $requestData["filters"][$recordSelectedKey] > 0 ) {
        $section->set_sectionRecordRecordset($requestData["filters"][$recordSelectedKey]);
      }

      // Create the json section data
      $jsonSectionData = $section->createJsonSectionData();
      $jsonResponseData = sprintf("\"section\":%s", $jsonSectionData);

      return $jsonResponseData;
    }

    //================================================================================================
    // Function processSqlInsertRecordRequest()
    //================================================================================================

    public function processSqlInsertRecordRequest($requestData) {
      
      $jsonResponseData = '';

      $className = sprintf("%sSection", $requestData["section"]["sectionType"]);
      $section = new $className($requestData["section"]);

      $recordStructure = $section->section["sectionRecord"]["sectionRecordStructure"];
      $recordRecordset = $requestData["section"]["sectionRecord"]["sectionRecordRecordset"];
      $affectedRows = $section->sectionSql->insertRecord($recordStructure, $recordRecordset);
      $jsonResponseData = sprintf('"section": { "sectionId": "%s", "affectedRows": "%s" }', $requestData["section"]["sectionId"], $affectedRows);

      return $jsonResponseData;
    }

    //================================================================================================
    // Function processSqlUpdateRecordRequest()
    //================================================================================================

    public function processSqlUpdateRecordRequest($requestData) {
      
      $jsonResponseData = '';

      $className = sprintf("%sSection", $requestData["section"]["sectionType"]);
      $section = new $className($requestData["section"]);

      $recordStructure = $section->section["sectionRecord"]["sectionRecordStructure"];
      $recordRecordset = $requestData["section"]["sectionRecord"]["sectionRecordRecordset"];
      $affectedRows = $section->sectionSql->updateRecord($recordStructure, $recordRecordset);
      $jsonResponseData = sprintf('"section": { "sectionId": "%s", "affectedRows": "%d" }', $requestData["section"]["sectionId"], $affectedRows);
      
      return $jsonResponseData;
    }

    //================================================================================================
    // Function processCloseRecordRequest()
    //================================================================================================

    public function processCloseRecordRequest($requestData) {
      
      $jsonResponseData = '';

      $jsonResponseData = sprintf('"section": { "sectionId": "%s" }', $requestData["section"]["sectionId"]);
            
      return $jsonResponseData;
    }

    //================================================================================================
    // Function sendResponse()
    //================================================================================================

    public function sendResponse($requestData, $jsonResponseData) {
      
      // Create json response data
      $responseJSON = <<<EOD
      { "action":"%s" %s}
      EOD;
      
      if ($jsonResponseData != '') { $responseData = sprintf(", %s ", $jsonResponseData); }
      $responseJSON = sprintf($responseJSON, $requestData["action"], $responseData);

      // Send response back
      echo $responseJSON;
    }

    //================================================================================================

  }
?>