<?php
/**
 * @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
 * @date 03/10/2017
 */

namespace AppBundle\Command;


use AppBundle\Util;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TripodoExportCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('sterntours:export-tripodo');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /** @var EntityManager $em */
        $em = $this->getContainer()->get('doctrine')->getManager();
        $logger = $this->getContainer()->get('logger');
        $regex = '/(\(\s*)?(((https?:\/\/)?www\.stern-?tours\.de[^\s\)]*)|([^\s]+@stern-?tours\.de)|(030\s*(-\s*)?700\s*94\s*100))(\s*\))?/';
        $baseUrl = 'https://www.sterntours.de';
        $xml = '<?xml version="1.0" encoding="UTF-8"?>' ."\n<travels>\n";

        $travelPrograms = $em->getRepository('AppBundle:TravelProgram')->getProgramsToExport();
        foreach ($travelPrograms as $travelProgram)
        {
            $em->getRepository('AppBundle:TravelPeriod')->getTrueTravelPeriods($travelProgram);
            $vars = [
                'travel_program' => $travelProgram,
                'base_url' => $this->getContainer()->getParameter('base_url'),
            ];

            // Link / E-Mail / Tel. zu Stern Tours entfernen
            $vars['html_description'] = preg_replace($regex, '', $travelProgram->getHtmlDescription());
            $vars['included'] = preg_replace($regex, '', $travelProgram->getIncluded());
            $vars['advices'] = preg_replace($regex, '', $travelProgram->getAdvices());

            $vars['included'] = Util::convertUrisInHtmlToAbsolute($vars['included'], $baseUrl);
            $vars['advices'] =  Util::convertUrisInHtmlToAbsolute($vars['advices'], $baseUrl);
            $vars['subtitle'] = Util::convertUrisInHtmlToAbsolute($travelProgram->getSubtitle(), $baseUrl);

            // Video-Link entfernen
            $vars['html_description'] = preg_replace('/<p>\s*<a.*?href\s*="#".*?<\/a>\s*<\/p>/', '', $vars['html_description']);

            // Überschriften hervorheben
            $vars['html_description'] = preg_replace('/<h2>(.*?)<\/h2>/', '<p><b>\1</b></p>', $vars['html_description']);
            $vars['html_description'] = preg_replace('/<h3>(.*?)<\/h3>/', '<p><b>\1</b></p>', $vars['html_description']);

            $vars['html_description'] = Util::convertUrisInHtmlToAbsolute(
                html_entity_decode($vars['html_description'], ENT_COMPAT, 'UTF-8'), $baseUrl);
            

            $xml .= $this->getContainer()->get('templating')->render('default/tripodo.xml.twig', $vars);
        }
        $xml .= '</travels>';

        file_put_contents('travels.xml', $xml);

        $host = $this->getContainer()->getParameter('tripodo_host');
        if (!$host)
        {
            $logger->error('Tripodo export failed. No host specified ("tripodo_host").');
            return;
        }
        $user = $this->getContainer()->getParameter('tripodo_user');
        if (!$host)
        {
            $logger->error('Tripodo export failed. No user name specified ("tripodo_user").');
            return;
        }
        $pass = $this->getContainer()->getParameter('tripodo_pass');
        if (!$pass)
        {
            $logger->error('Tripodo export failed. No password specified ("tripodo_pass").');
            return;
        }

        $conn = ftp_connect($host);
        if(ftp_login($conn, $user, $pass))
        {
            $stream = fopen('travels.xml', 'r');
            ftp_fput($conn, 'travels.xml', $stream, FTP_BINARY);
            fclose($stream);
        }
        ftp_close($conn);
    }


}