<?php
declare(strict_types=1);

namespace WP_AUX\Services\AuxFramework;

use WP_AUX\Entities\AuxFramework\Partials\AlertEntity;
use WP_AUX\Entities\AuxFramework\Partials\CaptionedMediaEntity;
use WP_AUX\Entities\AuxFramework\Partials\CardEntity;
use WP_AUX\Entities\AuxFramework\Partials\ContactCardEntity;
use WP_AUX\Entities\AuxFramework\Partials\DescriptionListEntity;
use WP_AUX\Entities\AuxFramework\Partials\IconListEntity;
use WP_AUX\Entities\AuxFramework\Partials\LinkedCardEntity;
use WP_AUX\Entities\AuxFramework\Partials\LinkListEntity;
use WP_AUX\Entities\AuxFramework\Partials\QaPairEntity;
use WP_AUX\Entities\AuxFramework\Partials\QuickFactEntity;
use WP_AUX\Entities\AuxFramework\Partials\TestimonialEntity;
use WP_AUX\Services\HtmlService;

class CustomContentSidebarService
{
  public static function createContactCard(): ContactCardEntity
  {
    return ContactCardEntity::create(
      get_field('sidebar_contact_card_name'),
      get_field('sidebar_contact_card_title'),
      get_field('sidebar_contact_card_org')
    )->setContactInfo(
      get_field('sidebar_contact_card_email'),
      get_field('sidebar_contact_card_phone'),
      get_field('sidebar_contact_card_profile_link')
    )->setAdminInfo(
      get_field('sidebar_contact_card_admin_name'),
      get_field('sidebar_contact_card_admin_title'),
      get_field('sidebar_contact_card_admin_email'),
      get_field('sidebar_contact_card_admin_phone')
    )->setImage(
      get_field('sidebar_contact_card_image')
    )->setLayout('sidebar');
  }

  public static function createCaptionedMedia(): ?CaptionedMediaEntity
  {
    $mediaType = aux_get_field('sidebar_captioned_media_type');

    $media = false;
    if ('image' === $mediaType) {
      $media = get_field('sidebar_captioned_media_image');
    } elseif ('video' === $mediaType) {
      $media = get_field('sidebar_captioned_media_video_link');
    }

    if (!$media) {
      return null;
    }

    return CaptionedMediaEntity::create(
      $media,
      $mediaType,
      aux_get_field('sidebar_captioned_media_caption'),
      aux_get_field('sidebar_captioned_media_credit')
    )->setIsSidebar(true);
  }

  public static function createAlert(): AlertEntity
  {
    return AlertEntity::create(
      get_field('sidebar_alert_type_select'),
      get_field('sidebar_alert_heading'),
      get_field('sidebar_alert_description')
    );
  }

  public static function createTestimonial(): TestimonialEntity
  {
    return TestimonialEntity::create(get_field('sidebar_testimonial_quote'))
      ->setContact(
        get_field('sidebar_testimonial_name'),
        get_field('sidebar_testimonial_title'),
        get_field('sidebar_testimonial_org')
      )->setImage(get_field('sidebar_testimonial_image'));
  }

  public static function createDescriptionList(): ?DescriptionListEntity
  {
    $descList = null;

    if ($descListItems = get_field('sidebar_desc_list')) {
      $descList = DescriptionListEntity::create();

      foreach ($descListItems as $descListItem) {
        $descList->addItem($descListItem['sidebar_desc_list_term'], $descListItem['sidebar_desc_list_desc']);
      }
    }

    return $descList;
  }

  public static function createLinkList(): ?LinkListEntity
  {
    $linkList = null;

    if ($linkListItems = get_field('sidebar_link_list')) {
      $linkList = LinkListEntity::create();

      foreach ($linkListItems as $link) {
        $linkList->addLink($link['sidebar_link_list_link'])->setDescription($link['sidebar_link_list_desc']);
      }
    }

    return $linkList;
  }

  public static function createIconList(): ?IconListEntity
  {
    $iconList = null;

    if ($iconListItems = get_field('sidebar_icon_list')) {
      $iconList = IconListEntity::create();

      foreach ($iconListItems as $iconListItem) {
        $iconList->addItem(
          $iconListItem['sidebar_icon_list_icon'],
          $iconListItem['sidebar_icon_list_item_text'],
          $iconListItem['sidebar_icon_list_desc']
        );
      }
    }

    return $iconList;
  }

  public static function createQuickFact(): QuickFactEntity
  {
    return QuickFactEntity::create(
      get_field('sidebar_freestanding_fact_heading'),
      get_field('sidebar_freestanding_fact_content')
    )
      ->setIcon(get_field('sidebar_freestanding_fact_icon'))
      ->setCitation(get_field('sidebar_freestanding_fact_citation'))
      ->setSuperheader(get_field('sidebar_freestanding_fact_superheader'))
      ->setSubheader(get_field('sidebar_freestanding_fact_subheader'));
  }

  public static function createLinkedCard(): LinkedCardEntity
  {
    return LinkedCardEntity::create(
      get_field('sidebar_linked_card_link'),
      get_field('sidebar_linked_card_heading'),
      get_field('sidebar_linked_card_content')
    )
      ->setIcon(get_field('sidebar_linked_card_icon'))
      ->setImage(get_field('sidebar_linked_card_image'))
      ->setSuperheader(get_field('sidebar_linked_card_superheader'))
      ->setSubheader(get_field('sidebar_linked_card_subheader'));
  }

  public static function createCard(): CardEntity
  {
    $card = CardEntity::createSidebar(
      get_field('sidebar_card_heading'),
      get_field('sidebar_card_content')
    )
      ->setSuperheader(get_field('sidebar_card_superheader'))
      ->setSubheader(get_field('sidebar_card_subheader'))
      ->setCtaType(get_field('sidebar_cards_cta_type'))
      ->setCta(get_field('sidebar_card_cta'))
      ->setImage(get_field('sidebar_card_image'))
      ->setIcon(get_field('sidebar_card_icon'));


    if ($cardLinks = get_field('sidebar_card_link_repeater')) {
      foreach ($cardLinks as $cta) {
        if ($cta) {
          $card->addCta($cta['sidebar_card_cta_link']);
        }
      }
    }

    return $card;
  }

  public static function createQaPair(): QaPairEntity
  {
    return QaPairEntity::create(
      get_field('sidebar_qa_pair_question'),
      get_field('sidebar_qa_pair_answer')
    )
      ->setLink(get_field('sidebar_qa_pair_link'))
      ->setShowLabels(get_field('sidebar_qa_pair_labels'));
  }

  public static function createSidebar(): string
  {
    switch (get_field('custom_content_sidebar_select')) {
      case 'alert':
        $sidebarContent = CustomContentSidebarService::createSidebarItem(
          CustomContentSidebarService::createAlert()
        );
        break;
      case 'captioned-media':
        $sidebarContent = CustomContentSidebarService::createSidebarItem(
          CustomContentSidebarService::createCaptionedMedia()
        );
        break;
      case 'card':
        $sidebarContent = CustomContentSidebarService::createSidebarItem(
          CustomContentSidebarService::createCard()
        );
        break;
      case 'linked-card':
        $sidebarContent = CustomContentSidebarService::createSidebarItem(
          CustomContentSidebarService::createLinkedCard()
        );
        break;
      case 'contact-card':
        $sidebarContent = CustomContentSidebarService::createContactCard();
        break;
      case 'free-fact':
        $sidebarContent = CustomContentSidebarService::createQuickFact();
        break;
      case 'qa-pair':
        $sidebarContent = CustomContentSidebarService::createSidebarItem(
          CustomContentSidebarService::createQaPair()
        );
        break;
      case 'link-list':
        $sidebarContent = CustomContentSidebarService::createSidebarListItem(
          CustomContentSidebarService::createLinkList(),
          'link-list'
        );
        break;
      case 'icon-list':
        $sidebarContent = CustomContentSidebarService::createSidebarListItem(
          CustomContentSidebarService::createIconList(),
          'icon-list'
        );
        break;
      case 'desc-list':
        $sidebarContent = CustomContentSidebarService::createSidebarListItem(
          CustomContentSidebarService::createDescriptionList(),
          'dl'
        );
        break;
      case 'testimonial':
        $sidebarContent = CustomContentSidebarService::createSidebarItem(
          CustomContentSidebarService::createTestimonial()
        );
        break;
      default:
        $sidebarContent = '';
    }

    return HtmlService::formattedValue($sidebarContent, '<div class="custom-content__sidebar">%s</div>');
  }

  public static function createSidebarItem(mixed $content): string
  {
    return HtmlService::formattedValue(
      $content,
      '<div class="sidebar-item">%s</div>'
    );
  }

  public static function createSidebarListItem(mixed $content, string $listType): string
  {
    $listHeading = HtmlService::formattedValue(
      get_field('sidebar_list_heading_text'),
      '<h2 class="list-heading">%s</h2><span class="divider-hash gold divider--mini"></span>'
    );

    return <<<HTML
      <div class="sidebar-item">
        <div class="{$listType}__wrapper">
          $listHeading
          $content
        </div>
      </div>
    HTML;
  }
}
