<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\Filter\InputFilter;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;

/**
 * Model class for tool.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardModelTool extends BaseDatabaseModel
{
	/**
	 * Create sample menu.
	 *
	 * @param   array  $data  Data.
	 *
	 * @return  mixed
	 *
	 * @since   1.0.0
	 */
	public function createSampleMenu($data)
	{
		$filter = new InputFilter;
		$db = $this->getDbo();
		$now = Factory::getDate()->toSql();
		$userId = Factory::getUser()->get('id');

		$menuTitle	= $filter->clean($data['sample_title'], 'string');
		$menuItemId	= $filter->clean($data['sample_menu_item'], 'uint');
		$articleId	= $filter->clean($data['sample_article'], 'uint');
		$picture	= $filter->clean($data['sample_picture'], 'string');
		$sampleText	= Text::_('COM_CMGIFTCARD_SAMPLE_TEXT');

		// Create menu.
		$menu = new stdClass;
		$menu->title = ($menuTitle == '') ? 'Sample menu' : $menuTitle;
		$menu->created = $now;
		$menu->created_by = $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_menus', $menu, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		// Create classic single menu item.
		$pSCls					= new StdClass;
		$pSCls->title			= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_CLASSIC');
		$pSCls->menu_id			= $menu->id;
		$pSCls->menu_item_id	= $menuItemId;
		$pSCls->type			= 'classic';
		$pSCls->ordering		= 9;
		$pSCls->published		= 1;
		$pSCls->created			= $now;
		$pSCls->created_by		= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pSCls, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		// Create classic dropdown menu item.
		$pCls				= new StdClass;
		$pCls->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_CLASSIC_DROPDOWN');
		$pCls->menu_id		= $menu->id;
		$pCls->type			= 'classic';
		$pCls->ordering		= 8;
		$pCls->published	= 1;
		$pCls->created		= $now;
		$pCls->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pCls, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$ordering = 5;

		for ($i = 1; $i <= 5; $i++)
		{
			$n = ($i > 3) ? $i - 1 : $i;
			$cls				= new StdClass;
			$cls->title			= ($i != 3) ?
				Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_CLASSIC_ITEM', $n) :
				Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_DIVIDER');
			$cls->menu_id		= $menu->id;
			$cls->parent_id		= $pCls->id;
			$cls->menu_item_id	= $menuItemId;
			$cls->type			= 'classic_item';
			$cls->value			= ($i != 3) ? 'link' : 'divider';
			$cls->ordering		= $ordering--;
			$cls->published		= 1;
			$cls->created		= $now;
			$cls->created_by	= $userId;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $cls, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}
		}

		// Create section.
		$pSec				= new StdClass;
		$pSec->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_LIST');
		$pSec->menu_id		= $menu->id;
		$pSec->type			= 'section';
		$pSec->ordering		= 7;
		$pSec->published	= 1;
		$pSec->created		= $now;
		$pSec->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pSec, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		// Create sections.
		$ordering = 4;

		for ($i = 1; $i <= 4; $i++)
		{
			$sec				= new StdClass;
			$sec->title			= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_SECTION', $i);
			$sec->menu_id		= $menu->id;
			$sec->parent_id		= $pSec->id;
			$sec->type			= 'section_title';
			$sec->ordering		= $ordering--;
			$sec->published		= 1;
			$sec->created		= $now;
			$sec->created_by	= $userId;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $sec, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}

			$this->createListItem($menu->id, $sec->id, $menuItemId, $now, $userId, $i);
		}

		// Create accordion.
		$pAcc				= new StdClass;
		$pAcc->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_ACCORDION');
		$pAcc->menu_id		= $menu->id;
		$pAcc->type			= 'accordion';
		$pAcc->full_width	= 1;
		$pAcc->ordering		= 6;
		$pAcc->published	= 1;
		$pAcc->created		= $now;
		$pAcc->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pAcc, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$ordering = 2;

		for ($i = 1; $i <= 2; $i++)
		{
			$acc				= new StdClass;
			$acc->title			= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_ACCORDION_ITEM', $i);
			$acc->menu_id		= $menu->id;
			$acc->parent_id		= $pAcc->id;
			$acc->type			= 'accordion_group';
			$acc->ordering		= $ordering--;
			$acc->published		= 1;
			$acc->created		= $now;
			$acc->created_by	= $userId;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $acc, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}

			$this->createCollapsibleItem($menu->id, $acc->id, $sampleText, $now, $userId);
		}

		// Create pictures.
		$pPic				= new StdClass;
		$pPic->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_PICTURES');
		$pPic->menu_id		= $menu->id;
		$pPic->type			= 'picture';
		$pPic->full_width	= 1;
		$pPic->ordering		= 5;
		$pPic->published	= 1;
		$pPic->created		= $now;
		$pPic->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pPic, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$ordering = 6;

		for ($i = 1; $i <= 6; $i++)
		{
			$pic				= new StdClass;
			$pic->title			= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_PICTURE_ITEM', $i);
			$pic->menu_id		= $menu->id;
			$pic->parent_id		= $pPic->id;
			$pic->menu_item_id	= $menuItemId;
			$pic->type			= 'picture_item';
			$pic->image			= $picture;
			$pic->ordering		= $ordering--;
			$pic->published		= 1;
			$pic->created		= $now;
			$pic->created_by	= $userId;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $pic, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}
		}

		// Create Media.
		$pMed				= new StdClass;
		$pMed->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_MEDIA');
		$pMed->menu_id		= $menu->id;
		$pMed->type			= 'media';
		$pMed->full_width	= 1;
		$pMed->ordering		= 4;
		$pMed->published	= 1;
		$pMed->created		= $now;
		$pMed->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pMed, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$ordering = 3;

		for ($i = 1; $i <= 3; $i++)
		{
			$med				= new StdClass;
			$med->title			= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_MEDIA_ITEM', $i);
			$med->menu_id		= $menu->id;
			$med->parent_id		= $pMed->id;
			$med->menu_item_id	= $menuItemId;
			$med->type			= 'media_item';
			$med->value			= $sampleText;
			$med->image			= $picture;
			$med->ordering		= $ordering--;
			$med->published		= 1;
			$med->created		= $now;
			$med->created_by	= $userId;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $med, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}
		}

		// Create Thumbnails.
		$pThumb				= new StdClass;
		$pThumb->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_THUMBNAILS');
		$pThumb->menu_id	= $menu->id;
		$pThumb->type		= 'thumbnail';
		$pThumb->full_width	= 1;
		$pThumb->ordering	= 3;
		$pThumb->published	= 1;
		$pThumb->created	= $now;
		$pThumb->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pThumb, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$ordering = 4;

		for ($i = 1; $i <= 4; $i++)
		{
			$thumb					= new StdClass;
			$thumb->title			= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_THUMBNAIL_ITEM', $i);
			$thumb->menu_id			= $menu->id;
			$thumb->parent_id		= $pThumb->id;
			$thumb->menu_item_id	= $menuItemId;
			$thumb->type			= 'thumbnail_item';
			$thumb->button_label	= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_VIEW');
			$thumb->value			= $sampleText;
			$thumb->image			= $picture;
			$thumb->ordering		= $ordering--;
			$thumb->published		= 1;
			$thumb->created			= $now;
			$thumb->created_by		= $userId;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $thumb, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}
		}

		// Create HTML
		$pHtml				= new StdClass;
		$pHtml->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_HTML');
		$pHtml->menu_id		= $menu->id;
		$pHtml->type		= 'html';
		$pHtml->full_width	= 1;
		$pHtml->ordering	= 2;
		$pHtml->published	= 1;
		$pHtml->created		= $now;
		$pHtml->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pHtml, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$html1					= new StdClass;
		$html1->title			= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_HTML');
		$html1->menu_id			= $menu->id;
		$html1->parent_id		= $pHtml->id;
		$html1->menu_item_id	= 0;
		$html1->type			= 'html_item';
		$html1->value			= '<div style="color: blue; font-weight: bold;">' . $sampleText . '</div>';
		$html1->ordering		= 1;
		$html1->published		= 1;
		$html1->created			= $now;
		$html1->created_by		= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $html1, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$html2					= new StdClass;
		$html2->title			= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_HTML_PLUGIN');
		$html2->menu_id			= $menu->id;
		$html2->parent_id		= $pHtml->id;
		$html2->menu_item_id	= 0;
		$html2->type			= 'html_item';
		$html2->value			= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_HTML_PLUGIN_VALUE');
		$html1->ordering		= 1;
		$html2->published		= 1;
		$html2->created			= $now;
		$html2->created_by		= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $html2, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		// Create Article.
		$pArticle				= new StdClass;
		$pArticle->title		= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_ARTICLE');
		$pArticle->menu_id		= $menu->id;
		$pArticle->type			= 'article';
		$pArticle->full_width	= 1;
		$pArticle->ordering		= 1;
		$pArticle->published	= 1;
		$pArticle->created		= $now;
		$pArticle->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $pArticle, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		$article				= new StdClass;
		$article->title			= Text::_('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_ARTICLE');
		$article->menu_id		= $menu->id;
		$article->parent_id		= $pArticle->id;
		$article->menu_item_id	= 0;
		$article->type			= 'article_item';
		$article->value			= $articleId;
		$article->published		= 1;
		$article->created		= $now;
		$article->created_by	= $userId;

		try
		{
			$db->insertObject('#__cmgiftcard_items', $article, 'id');
		}
		catch (RuntimeException $e)
		{
			return CMGiftCardHelper::throwException($e->getMessage(), 500);

			return false;
		}

		return true;
	}

	/**
	 * Create list item.
	 *
	 * @param   integer  $menuId      Menu ID
	 * @param   integer  $sectionId   Section ID
	 * @param   integer  $menuItemId  Joomla menu item ID
	 * @param   string   $created     Created time
	 * @param   integer  $createdBy   User ID
	 * @param   integer  $order       Section's ordering
	 *
	 * @return  boolean
	 *
	 * @since   1.0.0
	 */
	protected function createListItem($menuId, $sectionId, $menuItemId, $created, $createdBy, $order)
	{
		$db = Factory::getDbo();

		$j = ($order != 4) ? 4 : 2;

		$ordering = $j;

		for ($i = 1; $i <= $j; $i++)
		{
			$item				= new StdClass;
			$item->title		= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_LIST_ITEM', $i);
			$item->menu_id		= $menuId;
			$item->parent_id	= $sectionId;
			$item->menu_item_id	= ($order != 2) ? $menuItemId : 0;
			$item->type			= 'section_title';
			$item->ordering		= $ordering--;
			$item->published	= 1;
			$item->created		= $created;
			$item->created_by	= $createdBy;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $item, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}

			if ($order == 4 && $j == 2 && $i == 2)
			{
				for ($k = 1; $k <= 2; $k++)
				{
					$sitem					= new StdClass;
					$sitem->title			= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_LIST_ITEM', $i + $k);
					$sitem->menu_id			= $menuId;
					$sitem->parent_id		= $item->id;
					$sitem->menu_item_id	= $menuItemId;
					$sitem->type			= 'section_title';
					$sitem->published		= 1;
					$sitem->created			= $created;
					$sitem->created_by		= $createdBy;

					try
					{
						$db->insertObject('#__cmgiftcard_items', $sitem, 'id');
					}
					catch (RuntimeException $e)
					{
						return CMGiftCardHelper::throwException($e->getMessage(), 500);

						return false;
					}
				}
			}
		}
	}

	/**
	 * Create collapsible item.
	 *
	 * @param   integer  $menuId      Menu ID
	 * @param   integer  $sectionId   Section ID
	 * @param   string   $sampleText  Sample text
	 * @param   string   $created     Created time
	 * @param   integer  $createdBy   User ID
	 *
	 * @return  boolean
	 *
	 * @since   1.0.0
	 */
	protected function createCollapsibleItem($menuId, $sectionId, $sampleText, $created, $createdBy)
	{
		$db = Factory::getDbo();

		$ordering = 4;

		for ($i = 1; $i <= 3; $i++)
		{
			$item				= new StdClass;
			$item->title		= Text::sprintf('COM_CMGIFTCARD_TOOL_SAMPLE_MENU_COLLAPSIBLE_ITEM', $i);
			$item->menu_id		= $menuId;
			$item->parent_id	= $sectionId;
			$item->type			= 'accordion_value';
			$item->value		= $sampleText;
			$item->ordering		= $ordering--;
			$item->published	= 1;
			$item->created		= $created;
			$item->created_by	= $createdBy;

			try
			{
				$db->insertObject('#__cmgiftcard_items', $item, 'id');
			}
			catch (RuntimeException $e)
			{
				return CMGiftCardHelper::throwException($e->getMessage(), 500);

				return false;
			}
		}

		return true;
	}
}
