<?php

namespace OfficeBrain\Bundle\CmsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; // Added additionaly to get countries

class CmsPageMasterType extends AbstractType
{
    private $container;

    public function __construct(ContainerInterface $container, array $fromAdmin = array())
    {
        $this->container = $container;
        $this->request = $this->container->get('request');
        $this->projectSetting = $this->request->get('project_setting');
        $this->subshopThemeManager = $this->container->get('office_brain_subshop_theme_manager');
        $this->translator = $this->container->get('translator');
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('slug')
            ->add('countryId', 'choice', array(
                    'label' => 'Country',
                    'choices' => $this->getCountryOptions($this->container->getParameter('country')),
                    'multiple' => false,
                    'required' => true,
                    'expanded' => true,
                    'disabled' => isset($options['data']) && $options['data']->getCountryId() > 0 ? true : false,
            ))
            ->add('status', 'choice', array(
                    'required' => true,
                      'multiple' => false,
                      'choices' => array(1 => 'Active', 0 => 'In Active'),
              ))
            ->add('languageMapping', 'collection', array('type' => new CmsPageLanguageType(), 'cascade_validation' => true))
            ->add('templateSlug', 'choice', array(
            		'label' => 'cms.admin.form.page.label.template_slug',
            		'choices' => $this->getTemplateList(),
            		'multiple' => false,
            ))
            ->add('css', 'textarea', array('required' => false));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OfficeBrain\Bundle\CmsBundle\Entity\CmsPageMaster', 'cascade_validation' => true,
            'error_bubbling' => true,
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'officebrain_bundle_cmsbundle_cmspagemaster';
    }

    private function getCountryOptions($countries)
    {
        $countryOptions = array();
        foreach ($countries as $country) {
            $countryOptions[] = array($country['id'] => $country['name']);
        }

        return $countryOptions;
    }
    
    private function getTemplateList()
    {
		$templateList = $themeData = array();
		
		$templateList = array('default' => $this->translator->trans('cms.admin.form.page.template.default'));

		$subshopInstanceMeta = $this->subshopThemeManager->getSubShopInstanceKey('theme_data', $this->projectSetting['instance_id']);
		
		if (count($subshopInstanceMeta)) {
			$themeData = json_decode($subshopInstanceMeta->getMetaValue(), 'Array');
			
			if (isset($themeData['cms_page_template'])) {
				$themeData = $themeData['cms_page_template'];
				
				foreach ($themeData as $templateSlug) {
					$templateList[$templateSlug] = $this->translator->trans('cms.admin.form.page.template.'.$templateSlug);
				}
			}
			
			
		}

		return $templateList;
	}
}
