<?php

/**
 * 
 */
class MioCommerceOlympusHomePageBuilder {
  public function buildPage($sections, $sectionsOrder, $servicesOutput, $chosenTheme, $pageUrls, $reviewPlatforms, $reusableBlocks) {

    $pageContent = '';
    foreach ($sectionsOrder as $sectionId) {
      foreach ($sections as $section) {
        if ($section['id'] != $sectionId || !$section['data']['isVisible']) continue;

        if ($section['type'] == 'booking-step') $pageContent .= $this->buildBookingStepSection($section);
        if ($section['type'] == 'services-section') $pageContent .= $this->buildServicesSection($servicesOutput, $chosenTheme);
        if ($section['type'] == 'information-section') $pageContent .= $this->buildInformationSection($section, $pageUrls['contact']);
        if ($section['type'] == 'testimonial-section') $pageContent .= $this->buildTestimonialSection($section);
        if ($section['type'] == 'review-contact-section') $pageContent .= $this->buildReviewSection($section, $pageUrls['contact'], $reviewPlatforms);
        if ($section['type'] == 'faq-home-section') $pageContent .= $this->buildFAQHomeSection($chosenTheme, $pageUrls['faq']);
        if ($section['type'] == 'why-choose-section') $pageContent .= $this->buildWhyChooseUsSection($reusableBlocks);
      }
    }

    return $pageContent;
  }

  private function buildBookingStepSection($section) {
    global $bookingStepPatternParams;

    $bookingStepPatternParams = [
      "theme" =>  $section['data']['selectedStyle'],
      "title" =>  $section['data']['title'],
      "description" =>  $section['data']['description'],
      "steps" =>  $section['data']['steps'],
    ];

    $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/booking-step.php';

    return $sectionContent;
  }

  private function buildServicesSection($servicesOutput, $chosenTheme) {
    global $servicesSectionPatternParams;

    $servicesSectionPatternParams = [
      "theme" =>  $chosenTheme,
      "servicesOutput" =>  $servicesOutput,
      "align" => ($chosenTheme == 'triumph') ? 'none' : 'wide'
    ];

    $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/services.php';

    return $sectionContent;
  }

  private function buildInformationSection($section, $contactPageUrl) {
    $theme = $section['data']['selectedStyle'];

    global $informationSectionPatternParams;
    $informationSectionPatternParams = [
      "image_id" => $section['data']['image']['attachment_id'],
      "image_url" => $section['data']['image']['url'],
      "points" => $section['data']['points'],
      "title" => $section['data']['sectionTitle'],
      "contact_page_url" => $contactPageUrl,
    ];

    if ($theme == 'triumph') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/information-section/triumph.php';
    } else if ($theme == 'accelerator') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/information-section/accelerator.php';
    } else {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/information-section/ace.php';
    }

    return $sectionContent;
  }

  private function buildTestimonialSection($section) {
    $theme = $section['data']['selectedStyle'];

    global $testimonialSectionPatternParams;
    $testimonialSectionPatternParams = [
      "title" => $section['data']['title'],
    ];

    if ($theme == 'triumph') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/testimonial-section/triumph.php';
    } else if ($theme == 'accelerator') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/testimonial-section/accelerator.php';
    } else {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/testimonial-section/ace.php';
    }

    return $sectionContent;
  }

  private function buildReviewSection($section, $contactPageUrl, $reviewPlatforms) {
    $theme = $section['data']['selectedStyle'];

    global $reviewSectionPatternParams;
    $reviewSectionPatternParams = [
      "title" => $section['data']['title'],
      "description" => $section['data']['description'],
      "contactTitle" => $section['data']['contactTitle'],
      "contactDescription" => $section['data']['contactDescription'],
      "contact_page_url" => $contactPageUrl,
      "review_platforms" => $reviewPlatforms
    ];

    if ($theme == 'triumph') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/review-section/triumph.php';
    } else if ($theme == 'accelerator') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/review-section/accelerator.php';
    } else {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/review-section/ace.php';
    }

    return $sectionContent;
  }

  private function buildFAQHomeSection($chosenTheme, $faqPageUrl) {
    if ($chosenTheme == 'triumph') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/faq-section/triumph.php';
    } else if ($chosenTheme == 'accelerator') {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/faq-section/accelerator.php';
    } else {
      $sectionContent = require plugin_dir_path(__FILE__) . 'patterns/home/faq-section/ace.php';
    }

    $sectionContent = str_replace('%%FAQ_PAGE_URL%%', $faqPageUrl, $sectionContent);

    return $sectionContent;
  }

  private function buildWhyChooseUsSection($reusableBlocks) {
    if (isset($reusableBlocks['whyChooseUs'])) {
      return '<!-- wp:block {"ref":' . $reusableBlocks['whyChooseUs'] . '} /-->';
    }
  }
}
