<?php

class AbstractLdapPreview
{

    protected $selectedItems;
    protected $regEx;
    protected $preview = array();

    public function __construct($ldapInterface)
    {
        $this->selectedItems = json_decode($ldapInterface->selectedItems, true);
        $this->regEx = json_decode($ldapInterface->regEx, true);
    }

    public function getPreviewDataProvider($ldapInterface)
    {
        $this->setPreviewContactsList($ldapInterface);
        $dataProvider = new CArrayDataProvider(
                $this->preview, array(
            'pagination' => false
        ));

        return $dataProvider;
    }

    protected function setPreviewContactsList($ldapInterface)
    {
        $ldapContacts = json_decode($ldapInterface->ldapData, true);
        if (!empty($ldapContacts)) {
            foreach ($ldapContacts as $key => $contact) {
                $formatedContact = $this->wrapContactData($contact);
                $phonalisaContact = $this->getLdapConnectedModels($ldapInterface->id, $formatedContact['ldapId']);
                if (!$this->contactIsEmpty($formatedContact)) {
                    if (isset($phonalisaContact)) {
                        $this->addContactToPreview($phonalisaContact, $formatedContact);
                    } else {
                        $this->prependContact($formatedContact, 'created');
                        unset($ldapContacts[$key]);
                    }
                }
            }
        }
    }

    protected function wrapContactData($ldapContact)
    {
        $contact = $this->selectedItems;
        foreach ($contact as $key => $attribute) {
            $value = isset($ldapContact[$attribute]) ? $ldapContact[$attribute] : "";
            $this->checkRegEx($attribute, $value, $contact);
            $contact[$key] = ($key == 'name') ? strtolower($value) : $value;
        }
        $contact['id'] = $this->createContactId();
        return $contact;
    }

    private function checkRegEx($attribute, &$value, &$contact)
    {
        if (!empty($this->regEx) && array_key_exists($attribute, $this->regEx)) {
            $expression = $this->regEx[$attribute];
            if (is_array($expression)) {
                $match = "/" . $expression[0] . "/";
                $replace = $expression[1];
                if (@preg_match($match, $value) === 1) {
                    $value .= " => " . preg_replace($match, $replace, $value);
                }
            }
        }
    }

    protected function getContactStatus($phonalisaContact, $ldapContact)
    {
        $attributes = array_keys($this->selectedItems);
        $status = 'upToDate';
        foreach ($attributes as $attribute) {
            if ($attribute == 'group') {
                continue;
            }
            if (property_exists($phonalisaContact, $attribute) && $phonalisaContact->{$attribute} != $ldapContact[$attribute]) {
                $status = 'updated';
            }
        }

        return $status;
    }

    protected function createContactId()
    {
        $id = count($this->preview);
        return "contactID_$id";
    }

    protected function getErrors($errors)
    {
        $errorList = "";
        foreach ($errors as $error) {
            $errorList .= "- $error[0] </br>";
        }

        return $errorList;
    }

    protected function contactIsEmpty($contact)
    {
        foreach ($contact as $key => $attribute) {
            if (!empty($attribute) && $key != 'id') {
                return false;
            }
        }
        return true;
    }

    protected function filterAttributesForValidation($contact)
    {
        foreach ($contact as $key => $value) {
            if (strpos($value, " => ")) {
                $contact[$key] = array_pop(explode(" => ", $value));
            }
        }
        return$contact;
    }
}
