<?php

$main_class = 'MailerLiteWindow';

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\MailerLiteBundle\MailerLite;

class MailerLiteWindow extends Window
{
    protected $action;
    protected $ID;
    protected $page;

    protected $template = 'window/mailerlite.tpl';
    protected $title = 'MailerLite';
    protected $data;

    /**
     * @var MailerLite
     */
    protected $mailerlite;

    public function __construct()
    {
        ini_set('memory_limit', '1G');
        $this->mailerlite = ServiceContainer::getService(MailerLite::class);
        $this->mailerlite->getSubscriberGroup();
    }

    public function get_vars()
    {
        if ($this->mailerlite->error) {
            $vars['ErrStr'] = $this->mailerlite->error;

            return $vars;
        }

        $this->ID = $this->getID();
        $this->action = $this->getAction();
        if ($this->action == 'edit') {
            $this->action = 'MailerLite';
        }
        $this->page = getVal('page', null, 0);
        $vars = parent::get_vars();

        $this->data['ShopUsers'] = $this->mailerlite->getKupShopUsers();
        $this->data['Unsubscribed'] = $this->mailerlite->getMailerLiteSubscribers_All('Unsubscribed');
        // $this->data['Active'] = $this->mailerlite->getMailerLiteSubscribers('Active', $this->page);
        $this->data['Junk'] = $this->mailerlite->getMailerLiteSubscribers('Junk');
        $this->data['Bounced'] = $this->mailerlite->getMailerLiteSubscribers('Bounced');
        $success = getVal('OkStr');
        $error = getVal('ErrStr');
        if ($success) {
            $this->data[$this->ID]['success'] = $success;
        }
        if ($error) {
            $this->data[$this->ID]['err'] = $error;
        }
        if ($this->ID == 'Synchronize') {
            if ($success) {
                if (!empty($success['success'])) {
                    $this->data[$this->ID]['success'] = $success['success'];
                }
                if (!empty($success['error'])) {
                    $this->data[$this->ID]['error'] = $success['error'];
                }
            }
            $this->ID = 'Unsubscribed';
        }
        $this->data['id'] = $this->ID;
        $this->data['page'] = $this->page;
        $this->data['limit'] = $this->mailerlite->limit;
        $flap = 'flap'.$this->ID;
        $vars['header']['flap'] = $flap;

        $pageVars['data'] = $this->data;
        $vars['body'] = $pageVars;
        $vars['body']['acn'] = $this->action;
        $vars['body']['type'] = $this->ID;
        $vars['body']['title'] = 'MailerLite Group '.$this->mailerlite->group['name'];
        $vars['body']['group'] = $this->mailerlite->group;

        return $vars;
    }

    public function handleShopUnsubscribe()
    {
        $this->ID = 'Unsubscribed';

        $data = getVal('unsubscribe');
        if (!empty($data)) {
            $ids = array_keys($data);
            $unsubscribe_data = $this->mailerlite->ShopUnsubscribe($ids);
            if (!empty($unsubscribe_data['success'])) {
                $this->returnOK($unsubscribe_data['success']);
            }
        }
    }

    public function handleShopSubscribe()
    {
        $this->ID = 'Active';

        $data = getVal('subscribe');
        if (!empty($data)) {
            $ids = array_keys($data);
            $subscribe_data = $this->mailerlite->ShopSubscribe($ids);
            if (!empty($subscribe_data['success'])) {
                $this->returnOK($subscribe_data['success']);
            }
        }
    }

    public function handleMailerliteUnsubscribe()
    {
        $this->ID = 'ShopUnsubscribed';

        if ($this->mailerlite->error) {
            return false;
        }

        $data = getVal('mailerlite_unsubscribe');
        if (!empty($data)) {
            $unsubscribe_data = $this->mailerlite->MailerliteUnsubscribe($data);

            if (!empty($unsubscribe_data['err'])) {
                $this->returnError($unsubscribe_data['err'], '', 'ShopUnsubscribed');
            }

            if (!empty($unsubscribe_data['success'])) {
                $this->returnOK($unsubscribe_data['success']);
            }
        }
    }

    public function handleMailerliteSubscribe()
    {
        $this->ID = 'ShopUsers';

        if ($this->mailerlite->error) {
            return false;
        }

        $data = getVal('export');
        if (!empty($data)) {
            $users = $this->mailerlite->getKupShopUsers($data);
            $subscribers = array_map([$this->mailerlite, 'prepareExportData'], $users['subscribers']);

            $subscribe_data = $this->mailerlite->MailerliteSubscribe($subscribers);

            if (!empty($subscribe_data['err'])) {
                $this->returnError($subscribe_data['err'], '', 'ShopUsers');
            }

            if (!empty($subscribe_data['success'])) {
                $this->returnOK($subscribe_data['success']);
            }
        }
    }

    public function handleMailerliteSynchronize()
    {
        $this->ID = 'Synchronize';

        if ($this->mailerlite->error) {
            return false;
        }

        $sync_data = $this->mailerlite->Synchronize();

        $this->returnOK($sync_data);
    }
}
