<?php

class vCardData extends PhonalisaRecord
{

    public $id;
    public $owner;
    public $iCloudContactsListID;
    public $vCardName;
    public $addressesId;
    public $addressBookId;
    public $data;
    public $eTag;
    public $changed;

    public static function myAttributes()
    {

        return array(
            'owner' => 'owner',
            'iCloudContactsListID' => 'iCloudContactsListID',
            'vCardName' => 'vCardName',
            'addressesId' => 'addressesId',
            'data' => 'data',
            'eTag' => 'eTag',
            'addressBookId' => 'addressBookId'
        );
    }

    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function rules()
    {
        return array(
            array('owner, iCloudContactsListID, vCardName, addressesId, data, eTag', 'required'),
            array(
                'owner, '
                . 'iCloudContactsListID, '
                . 'vCardName, '
                . 'addressesId, '
                .'addressBookId, '
                . 'data, '
                . 'eTag, ',
                'safe',
                'on' => 'update'
            )
        );
    }

    public function search($order = 'title')
    {
        $criteria = new CDbCriteria;
        $criteria->condition = 'owner=' . Yii::app()->user->sudoUser;

        $dataProvider = new CPhonalisaDataProvider($this, array(
            'criteria' => $criteria,
        ));
        return $dataProvider;
    }

    public function beforeDelete()
    {
        $deletedItem = new DeletedItem();
        $deletedItem->model_id = $this->id;
        $deletedItem->model_name = get_Class($this);
        $deletedItem->deleted = time();
        $this->removeVCardFromICloudContactsList();

        if ($deletedItem->save()) {
            return parent::beforeDelete();
        }
        return false;
    }

    public function createCopy($address)
    {
        $vCardCopy = new vCardData();
        $vCardCopy->vCardName = $address->id . '.vcf';
        $vCardCopy->attributes = $this->attributes;
        $vCardCopy->owner = $address->contact_owner;

        return $vCardCopy;
    }

    public function removeVCardFromICloudContactsList()
    {
        $ids = json_decode($this->iCloudContactsListID, true);
        if (!empty($ids)) {
            foreach ($ids as $id) {
                $iCloudContactsList = iCloudContactsList::model()->findByPk($id);
                if (isset($iCloudContactsList)) {
                    $iCloudContactsList->updateICloudContactsList('deletedLocalContacts', $this->vCardName);
                }
            }
        }
    }

    public function setNewICloudContactListIDs($addressBookId)
    {
        $iCloudContactsLists = iCloudContactsList::model()->findAllByAttributes(array('addressBookId' => $addressBookId));
        $newLists = array();
        if (!empty($iCloudContactsLists)) {
            foreach ($iCloudContactsLists as $iCloudContactsList) {
                if (isset($iCloudContactsList)) {
                    $newLists[] = $iCloudContactsList->id;
                }
            }
        }
        $this->iCloudContactsListID = json_encode($newLists);
        if (!$this->save()) {
            $this->createErrorLogMessages();
        }
    }

    public function afterDelete()
    {
        $address = Addresses::model()->findByPk($this->addressesId);
        if ($address) {
            $address->delete();
        }

        return parent::afterDelete();
    }

    private function createErrorLogMessages()
    {
        Log::e("An error has occurred saving vCardData-ID $this->id", 'iCloudSync [iCloudContactsList]');
        foreach ($this->errors as $key => $errorMsg) {
            Log::e("Error in $key ");
            if (is_array($errorMsg)) {
                Log::e("ErrorMessage in " . array_shift($errorMsg));
            }
        }
    }
}
