<?php
namespace MotoPlugin\MotoTeam\AdvancedContactForm;

use Moto;

class PluginConnector extends Moto\System\PluginConnector
{
    CONST PLUGIN_NAME = 'moto_team@advanced_contact_form';
    protected $_languages = array('en_US', 'ru_RU', 'de_DE');

    public function compatibleApiVersions()
    {
        return [1];
    }

    public function bootstrap()
    {
        $this->_connectWidgets();
        $this->_connectRpcMethods();
        $this->_connectNgModules();
        $this->_connectAssets();
    }

    protected function _connectWidgets()
    {
        Moto\System\Widgets\Factory::addWidget(array(
            'name' => 'moto_team@advanced_contact_form',
            'renderClass' => 'MotoPlugin\\MotoTeam\\AdvancedContactForm\\Widget',
            'renderFile' => '@plugins/moto_team/advanced_contact_form/library/Widget.php',
        ));
    }

    protected function _connectRpcMethods()
    {
        Moto\Json\Server::getInstance()->assignClass('MotoPlugin\MotoTeam\AdvancedContactForm\Service', 'MotoTeam.AdvancedContactForm');
    }

    protected function _connectNgModules()
    {
        Moto\Hook::on('application.plugins', function ($event, $value) {
            $value[] = 'moto_team@advanced_contact_form.backend';

            return $value;
        });

        Moto\Hook::on('website.plugins', function ($event, $value) {
            $value[] = 'moto_team@advanced_contact_form.frontend';

            return $value;
        });
    }

    protected function _connectAssets()
    {
        Moto\Hook::on('render.admin.head.top', function ($event, $value) {
            $twig = Moto\Render::getInstance();
            $motoAsset = $twig->getExtension('motoAsset');
            $value .= $motoAsset->assetJavaScript('@plugins/moto_team/advanced_contact_form/js/backend.min.js');
            $value .= $motoAsset->assetJavaScript('@plugins/moto_team/advanced_contact_form/js/templates.min.js');
            $value .= $motoAsset->assetJavaScript('@plugins/moto_team/advanced_contact_form/js/translations/en_US.min.js');
            $user = Moto\System::getUser();
            if ($user && $user->language_locale !== 'en_US' && in_array($user->language_locale, $this->_languages)) {
                $value .= $motoAsset->assetJavaScript('@plugins/moto_team/advanced_contact_form/js/translations/' . $user->language_locale . '.min.js');
            }
            $value .= $motoAsset->assetStyleSheet('@plugins/moto_team/advanced_contact_form/css/backend.min.css');

            return $value;
        });

        Moto\Hook::on('website_head_bottom', function ($event, $value) {
            $twig = Moto\Render::getInstance();
            $motoAsset = $twig->getExtension('motoAsset');

            if (Moto\System\Request::getQuery('mode') == 'preview') {
                $value .= $motoAsset->assetStyleSheet('@plugins/moto_team/advanced_contact_form/css/editor.min.css');
            }

            return $value;
        });

        if (Moto\System\Request::getQuery('mode') != 'preview') {
            Moto\Hook::on('website_body_bottom', function ($event, $value) {
                $twig = Moto\Render::getInstance();
                $motoAsset = $twig->getExtension('motoAsset');
                $value .= $motoAsset->assetJavaScript('@plugins/moto_team/advanced_contact_form/js/frontend.min.js');

                return $value;
            });
        }
    }
}
