<?php
/**
 *   @copyright Copyright (c) 2015 Quality Unit s.r.o.
 *   @author Martin Pullmann
 *   @package WpLiveAgentPlugin
 *   @version 1.0.0
 *
 *   Licensed under GPL2
 */

class liveagent_Form_Signup extends liveagent_Form_Base {
    private $userName = '';

    public function __construct(liveagent_Settings $settings) {
        $current_user = wp_get_current_user();
        $this->userName = $current_user->user_firstname . ' ' . $current_user->user_lastname;
        if ($this->userName === ' ') {
            $this->userName = $current_user->display_name;
        }
        if ($this->userName == '') {
            $this->userName = $current_user->user_login;
        }
        parent::__construct($settings);
        if (isset($_POST['la-error-message'])) {
            $this->setErrorMessages(array($_POST['la-error-message']));
        }
    }

    protected function getTemplateFile() {
        return $this->getTemplatesPath() . 'AccountSignup.xtpl';
    }

    private function getdomainOnly() {
        $fullDomain = @$_SERVER['SERVER_NAME'];

        while (preg_match('/^([A-Za-z0-9-_]+\.)+([A-Za-z]{2,6})$/', $fullDomain)) {
            $domainSuffix = preg_replace('/^([A-Za-z0-9-_]+\.)+([A-Za-z]{2,6})$/', '$2', $fullDomain);
            $fullDomain = str_replace('.' . $domainSuffix, '', $fullDomain);
        }
        $domain = str_replace('www.', '', $fullDomain);
        $domain = str_replace('.', '-', $domain);

        if (trim($domain) === 'localhost') {
            return '';
        }
        if (preg_match('/^[a-zA-Z0-9-_]+$/', $domain) === false) {
            return '';
        }
        return $domain;
    }

    protected function getOption($name) {
        switch ($name) {
            case liveagent_Settings::LA_OWNER_EMAIL_SETTING_NAME:
                if (isset($_POST[liveagent_Settings::LA_OWNER_EMAIL_SETTING_NAME]) && $_POST[liveagent_Settings::LA_OWNER_EMAIL_SETTING_NAME] != '') {
                    return $_POST[liveagent_Settings::LA_OWNER_EMAIL_SETTING_NAME];
                }
                return get_bloginfo('admin_email');
                break;
            case liveagent_Settings::LA_FULL_NAME:
                if (isset($_POST[liveagent_Settings::LA_FULL_NAME]) && $_POST[liveagent_Settings::LA_FULL_NAME] != '') {
                    return $_POST[liveagent_Settings::LA_FULL_NAME];
                }
                return $this->userName;
                break;
            case liveagent_Settings::LA_URL_SETTING_NAME:
                if (isset($_POST[liveagent_Settings::LA_URL_SETTING_NAME]) && $_POST[liveagent_Settings::LA_URL_SETTING_NAME] != '') {
                    return $_POST[liveagent_Settings::LA_URL_SETTING_NAME];
                }
                return $this->getdomainOnly();
                break;
        }
    }

    protected function initForm() {
        $this->addVariable('addLiveChat', __('Add Live Chat & Help Desk to your site', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('tryLaFor30', __('Try LiveAgent 30 days for free, no credit card required.', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('skipCreateUrl', admin_url('admin.php?page=' . liveagent_Form_Handler::TOP_LEVEL_OPTIONS_HANDLE . '&ac=' . liveagent_Settings::ACTION_SKIP_CREATE));
        $this->addVariable('startFreeTrial', 'https://www.liveagent.com/trial/?a_aid=wordpress&utm_source=wordpress&utm_medium=referral&utm_campaign=wordpress_plugin');
        $this->addVariable('pricing', 'https://www.liveagent.com/pricing/?a_aid=wordpress&utm_source=wordpress&utm_medium=referral&utm_campaign=wordpress_plugin');
        $this->addVariable('mainDomain', 'https://www.liveagent.com/?a_aid=wordpress&utm_source=wordpress&utm_medium=referral&utm_campaign=wordpress_plugin');
        $this->addVariable('signUp', __('Sign Up', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('login', __('Connect your account', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('startFreeTrialText', __('Start free trial', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('learnMore', __('Learn more', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('seePricing', __('See pricing', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('realTime', __('Connect with Visitors in Real Time with LiveAgent', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('integrate', __('Easily integrate LiveAgent into your WordPress website', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('provide', __('to provide seamless customer support', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature1', __('Live Chat with Visitor Tracking', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature2', __('AI Chatbot and Smart Automation', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature3', __('Call Center with VoIP Support', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature4', __('Multichannel Ticketing', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature5', __('Help Desk with Knowledge Base Support', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature6', __('Simple Integration and Customization', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature1text', __('Engage with website visitors and provide personalized support.', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature2text', __('Automate responses to common queries, freeing up your team\'s time for more complex issues.', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature3text', __('Receive, answer, record, and log phone calls as tickets, integrating VoIP services for seamless communication.', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature4text', __('Consolidate customer inquiries into a unified ticketing system.', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature5text', __('Create a customizable knowledge base for your customers, offering self-service portals, articles, forums, and feedback boards.', LIVEAGENT_PLUGIN_NAME));
        $this->addVariable('feature6text', __('Customize the plugin to match your website\'s design and workflow.', LIVEAGENT_PLUGIN_NAME));

        $this->addVariable(liveagent_Settings::LA_OWNER_EMAIL_SETTING_NAME, $this->getOption(liveagent_Settings::LA_OWNER_EMAIL_SETTING_NAME));
        $this->addVariable(liveagent_Settings::LA_FULL_NAME, $this->getOption(liveagent_Settings::LA_FULL_NAME));
        $this->addVariable(liveagent_Settings::LA_URL_SETTING_NAME, $this->getOption(liveagent_Settings::LA_URL_SETTING_NAME));
    }
}