<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Server_Utils_Mail extends Francis_Utils_Mail {

    public function __construct()
    {
        $this->setFrom( 'noreply@probuddy.de' );
        $this->CharSet = 'utf-8';
        $this->isHtml( true );
    }

    /**
     * Send email validation mail
     *
     * @param array $data
     *
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendEmailValidation( array $data )
    {
        $tpl = new TB_Server_Utils_Template( 'mail/email-validation' );

        $mail = new self();
        $mail->Subject = 'ProBuddy - Account Validierung';
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $tpl->get( $data ) );

        if ( false === Francis_Utils_Helper::isWindows() )
        {
            if ( false === $mail->send() )
            {
                TB_Server_Utils_Log::get()->log( 'Could not send mail' );
            }
        }
    }

    /**
     * Send email validation mail
     *
     * @param array $data
     *
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendEmailValidationTrainer( array $data )
    {
        $tpl = new TB_Server_Utils_Template( 'mail/email-validation-trainer' );

        $mail = new self();
        $mail->Subject = 'ProBuddy - Account Validierung';
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $tpl->get( $data ) );

        if ( false === Francis_Utils_Helper::isWindows() )
        {
            if ( false === $mail->send() )
            {
                TB_Server_Utils_Log::get()->log( 'Could not send mail' );
            }
        }
    }

    /**
     * Send password reset mail
     *
     * @param array $data
     *
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendPasswordReset( array $data )
    {
        $tpl = new TB_Server_Utils_Template( 'mail/password-reset' );

        $mail = new self();
        $mail->Subject = 'ProBuddy - Passwort zurücksetzen';
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $tpl->get( $data ) );

        if ( false === Francis_Utils_Helper::isWindows() )
        {
            $mail->send();
        }
    }



    /**
     * Send invitation mail
     *
     * @param array $data
     *
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendInvitationMail( array $data )  {

        $success = false;

        $tpl = new TB_Server_Utils_Template( 'mail/invitation2' );

        $mail = new self();
        $mail->Subject = 'TBuddy - Einladung zum Team ' . $data[ 'team_display_name' ];
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $tpl->get( $data ), 'Teameinladung' );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $success = $mail->send();
        } else {
            $success = true;
            TB_Server_Utils_Log::get()->log( $mail->Body );
        }

        return $success;
    }

    /**
     * Sends email for email change
     *
     * @param array $data
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendEmailChange( array $data ) {
        // Send email
        $tpl = new TB_Server_Utils_Template( 'mail/emailchange2' );
        $tplVars = array();
        $tplVars[ 'cec_link' ] = TB_Server_Core_Request::createClientUrl(
            'Auth',
            'confirm_email_change',
            array( 't' => $data[ 'token' ] )
        );

        $mail = new self();
        $mail->Subject = 'TBuddy - EMail Adresse ändern';
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $tpl->get( $tplVars ), 'EMail Adresse Ändern' );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $mail->send();
        }
    }

    /**
     * Send email notification mail
     *
     * @param array $data
     *
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendEmailNotification( array $data )  {
        $success = true;

        $mail = new self();
        $mail->Subject = $data[ 'subject' ];
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $data[ 'content' ] );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $success = $mail->send();
            if ( false == $success ) {
                TB_Server_Utils_Log::get()->log( "Send notification error: " . $mail->ErrorInfo );
            }
        }

        return $success;
    }

    /**
     *
     * Send email after coach successfully validated his email
     *
     * @param array $data
     * @return bool
     */
    public static function sendWelcomeTeam( array $data ) {
        $success = true;

        $tpl = new TB_Server_Utils_Template( 'mail/welcome-newteam' );
        $content = $tpl->get();

        $mail = new self();
        $mail->Subject = 'Willkommen bei TBuddy!';
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $content );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $success = $mail->send();
            if ( false == $success ) {
                TB_Server_Utils_Log::get()->log( "Send notification error: " . $mail->ErrorInfo );
            }
        }

        return $success;
    }

    /**
     * @param TB_Shared_Ent_Core_Account $entAccount
     * @param array $data
     * @return bool
     */
    public static function sendReminder2( TB_Shared_Ent_Core_Account $entAccount, array $data )
    {
        $success = true;

        $tpl = new TB_Server_Utils_Template( 'mail/reminder2' );
        $content = $tpl->get( $data );

        $mail = new self();
        $mail->Subject = 'Dein Start bei TBuddy!';
        $mail->addAddress( $entAccount->email );
        $mail->Body = self::wrapHtml( $content );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $success = $mail->send();
            if ( false == $success ) {
                TB_Server_Utils_Log::get()->log( "Send notification error: " . $mail->ErrorInfo );
            }
        }

        return $success;
    }

    /**
     * @param TB_Shared_Ent_Core_Account $entAccount
     * @param array $data
     * @return bool
     */
    public static function sendReminder4( TB_Shared_Ent_Core_Account $entAccount, array $data )
    {
        $success = true;

        $tpl = new TB_Server_Utils_Template( 'mail/reminder4' );
        $content = $tpl->get( $data );

        $mail = new self();
        $mail->Subject = 'Die nächsten Schritte bei TBuddy!';
        $mail->addAddress( $entAccount->email );
        $mail->Body = self::wrapHtml( $content );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $success = $mail->send();
            if ( false == $success ) {
                TB_Server_Utils_Log::get()->log( "Send notification error: " . $mail->ErrorInfo );
            }
        }

        return $success;
    }

    /**
     * @param TB_Shared_Ent_Core_Account $entAccount
     * @param array $data
     * @return bool
     */
    public static function sendReminder8( TB_Shared_Ent_Core_Account $entAccount, array $data )
    {
        $success = true;

        $tpl = new TB_Server_Utils_Template( 'mail/reminder8' );
        $content = $tpl->get( $data );

        $mail = new self();
        $mail->Subject = 'Dein Stand nach einer Woche bei TBuddy!';
        $mail->addAddress( $entAccount->email );
        $mail->Body = self::wrapHtml( $content );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $success = $mail->send();
            if ( false == $success ) {
                TB_Server_Utils_Log::get()->log( "Send notification error: " . $mail->ErrorInfo );
            }
        }

        return $success;
    }

    /**
     * @param array $data
     * @return bool
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendDeleteConfirmation( array $data ) {

        $success = true;
        $tpl = new TB_Server_Utils_Template( 'mail/deleteconfirmation' );
        $content = $tpl->get( $data );

        $mail = new self();
        $mail->Subject = $data[ 'subject' ];
        $mail->addAddress( $data[ 'email' ] );
        $mail->Body = self::wrapHtml( $content );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $success = $mail->send();
            if ( false == $success ) {
                TB_Server_Utils_Log::get()->log( "Send notification error: " . $mail->ErrorInfo );
            }
        }

        return $success;

    }

    /**
     * Send logs to personal email address
     *
     * @param string $logs
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendLogs( $logs ) {
        $mail = new self();
        $mail->Subject = 'TBuddy -  Logs: ' . date( 'Y-m-d' );
        $mail->addAddress( 'info@bennyschuetz.com' );
        $mail->Body = $logs;

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $mail->send();
        }
    }

    /**
     * Send mail to support
     * @param string $subject
     * @param string $body
     * @throws phpmailerException
     */
    public static function sendToSupport( $subject, $body ) {
        $mail = new self();
        $mail->Subject = $subject;
        $mail->addAddress( 'support@probuddy.de' );
        $mail->Body = nl2br( $body );

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $mail->send();
        }
    }

    /**
     * Send stats
     *
     * @param $stats
     * @throws Exception
     * @throws phpmailerException
     */
    public static function sendStats( $stats ) {
        $mail = new self();
        $mail->Subject = 'TBuddy -  Stats: ' . date( 'Y-m-d' );
        $mail->addAddress( 'timo@tbuddy.de' );
        $mail->addAddress( 'benny@tbuddy.de' );
        $mail->Body = $stats;

        if ( false === Francis_Utils_Helper::isWindows() ) {
            $mail->send();
        }
    }

    /**
     * Wrap mail content in HTML template
     *
     * @param string $mailContent
     * @return string
     */
    private static function wrapHtml( $mailContent )
    {
        $tpl = new TB_Server_Utils_Template( 'mail/layout' );
        return $tpl->get( array(
            'mail_content' => $mailContent
        ));
    }
} 