<?php
/**
 * app/controllers/admin/send_mail.php
 *
 * This is the admin email controller. The only real use is to send out emails to the various lists.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers\Admin;

use TheTempusProject\Classes\AdminController;
use TheTempusProject\Classes\Email;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Models\User;
use TheTempusProject\Models\Subscribe;
use TheTempusProject\Plugins\Subscribe as Plugin;

class SendMail extends AdminController {
    public static $user;
    public static $subscribe;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Send Mail';
        self::$user = new User;

        if ( class_exists( 'TheTempusProject\Plugins\Subscribe' ) ) {
            $plugin = new Plugin;
            if ( ! $plugin->checkEnabled() ) {
                Issues::add( 'notice', 'Subscriptions are disabled so those feature will be unavailable.' );
            } else {
                self::$subscribe = new Subscribe;
            }
        } else {
            Issues::add( 'notice', 'Subscriptions plugin is not installed so those feature will be unavailable.' );
        }
    }

    private function emailSubscribers( $params ) {
        if ( empty( self::$subscribe ) ) {
            Issues::add( 'error', 'Subscriptions plugin is unavailable' );
            return;
        }
        $list = self::$subscribe->list();
        if ( empty( $list ) ) {
            Issues::add( 'error', 'No subscribers found' );
            return;
        }
        foreach ( $list as $recipient ) {
            $params[ 'confirmationCode' ] = $recipient->confirmationCode;
            Email::send( $recipient->email, 'contact', $params, [ 'template' => true, 'unsubscribe' => true ] );
        }
    }

    private function emailUsers( $params, $limit = null ) {
        $list = self::$user->userList( $limit );
        foreach ( $list as $recipient ) {
            Email::send( $recipient->email, 'contact', $params, [ 'template' => true ] );
        }
    }

    public function index() {
        if ( Input::exists( 'mailType' ) ) {
            $params = [
                'subject' => Input::post( 'mailSubject' ),
                'title' => Input::post( 'mailTitle' ),
                'message' => Input::post( 'mailMessage' ),
            ];
            switch ( Input::post( 'mailType' ) ) {
                case 'registered':
                    $this->emailUsers( $params );
                    Issues::add( 'success', 'Email(s) Sent' );
                    break;

                case 'newsletter':
                    $this->emailUsers( $params, 'newsletter' );
                    Issues::add( 'success', 'Email(s) Sent' );
                    break;

                case 'all':
                    $this->emailUsers( $params );
                    $this->emailSubscribers( $params );
                    Issues::add( 'success', 'Email(s) Sent' );
                    break;

                case 'opt':
                    $this->emailUsers( $params, 'newsletter' );
                    $this->emailSubscribers( $params );
                    Issues::add( 'success', 'Email(s) Sent' );
                    break;

                case 'subscribers':
                    $this->emailSubscribers( $params );
                    Issues::add( 'success', 'Email(s) Sent' );
                    break;

                default:
                    Issues::add( 'error', 'Invalid Request' );
                    break;
            }
        }
        Views::view( 'admin.contact' );
    }
}
