<?php

class iCalData extends PhonalisaRecord
{

    public $id;
    public $owner;
    public $iCalContactListID;
    public $iCalName;
    public $eventId;
    public $calendarId;
    public $data;
    public $eTag;
    public $changed;

    public static function myAttributes()
    {

        return array(
            'owner' => 'owner',
            'iCalContactListID' => 'iCalContactListID',
            'iCalName' => 'iCalName',
            'eventId' => 'eventId',
            'data' => 'data',
            'eTag' => 'eTag',
            'calendarId' => 'calendarId'
        );
    }

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

    public function rules()
    {
        return array(
            array('owner, iCalContactListID, iCalName, eventId, data, eTag', 'required'),
            array(
                'owner, '
                . 'iCalContactListID, '
                . 'iCalName, '
                . 'eventId, '
                .'calendarId, '
                . '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($event)
    {
        $iCalCopy = new iCalData();
        $iCalCopy->iCalName = $event->id . '.vcf';
        $iCalCopy->attributes = $this->attributes;
        $iCalCopy->owner = $event->owner;

        return $iCalCopy;
    }

    public function removeVCardFromICloudContactsList()
    {    
        $ids = json_decode($this->iCalContactListID, true);
        
        if (!empty($ids)) {            
            foreach ($ids as $id) {                
                $iCalContactList = iCalContactList::model()->findByPk($id);   
                if (isset($iCalContactList)) {
                    $iCalContactList->updateICalContactsList('deletedLocalContacts', $this->iCalName);
                }
            }
        }
    }

    public function setNewICloudContactListIDs($calendarId)
    {
        $iCalContactLists = iCalContactList::model()->findAllByAttributes(array('calendarId' => $calendarId));
        $newLists = array();
        if (!empty($iCalContactLists)) {
            foreach ($iCalContactLists as $iCalContactList) {
                if (isset($iCalContactList)) {
                    $newLists[] = $iCalContactList->id;
                }
            }
        }
        $this->iCalContactListID = json_encode($newLists);
        if (!$this->save()) {
            $this->createErrorLogMessages();
        }
    }

    public function afterDelete()
    {
        $event = CalendarEvent::model()->findByPk($this->eventId);
        
        if ($event) {
            $event->delete();
        }

        return parent::afterDelete();
    }

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