<?php

class TiersType
{

    const user = 0;
    const external = 1;

    public static function getTiersType($type)
    {
        switch ($type) {
            case TiersType::user :
                $typeAsString = __('TIERS_MODEL_TIERS_TYPE_USER');
                break;
            case TiersType::external :
                $typeAsString = __('TIERS_MODEL_TIERS_TYPE_EXTERNAL');
                break;
        }
        return $typeAsString;
    }
}

class Tiers extends PhonalisaRecord
{

    public $userId;
    public $queueId;
    public $level;
    public $position = '1';
    public $type;
    public $static_member;
    public $loggedIn;
    public $timeout = '10';
    public $tempUserExtension;
    public $queueExtension;

    public static function myAttributes()
    {
        return array(
            'userId' => 'userId',
            'queueId' => 'queueId',
            'level' => 'level',
            'position' => 'position',
            'type' => 'type',
            'static_member' => 'static_member',
            'loggedIn' => 'loggedIn',
            'timeout' => 'timeout',
            "tempUserExtension" => "tempUserExtension",
            'queueExtension' => 'queueExtension'
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Job the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'Tiers';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('userId, queueId', 'required'),
            //array('timeout', 'numerical', 'integerOnly'=>true),
            array('timeout', 'numerical', 'integerOnly' => true),
            array('level, position,loggedIn , type, static_member,loggedIn, tempUserExtension, queueExtension', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('userId, loggedIn, queueId, level, position, type, static_member,timeout', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'userId' => __('TIERS_MODEL_USER_ID'),
            'externalAgentId' => __('TIERS_MODEL_EXTERNAL_AGENT_ID'),
            'queueId' => __('TIERS_MODEL_QUEUE_ID'),
            'level' => __('TIERS_MODEL_LEVEL'),
            'position' => __('TIERS_MODEL_POSITION'),
            'type' => __('TIERS_MODEL_TYPE'),
            'queue' => __('TIERS_MODEL_QUEUE'),
            'static_member' => __('TIERS_MODEL_STATIC_MEMBER'),
            'timeout' => __('TIERS_MODEL_TIMEOUT'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CPhonalisaDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($userId)
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        //FIXME
        $models = $this->findAllByAttributes(array('userId' => $userId));
        if (empty($models)) {

            $models[] = $this;
        }
        return new CArrayDataProvider($models, array('pagination' => false));
    }

    public function searchQueueMembers($queueId, $type)
    {
        $tiers = $this->findAllByAttributes(array('queueId' => $queueId, 'type' => $type));
        if (empty($tiers)) {
            $this->type = TiersType::user;
            $tiers = array($this);
        }
        return new CArrayDataProvider($tiers, array('pagination' => false));
    }

    public function getQueueDropDownData($userId = NULL)
    {
        $user = NULL;
        $models = Queue::model()->findAll();
        $list = array();

        if ($userId) {
            $user = User::model()->findByPk($userId);
            $exsistingQueues = $user->getQueueIds();
        }

        foreach ($models as $queue) {
            if ($user) {
                if (!in_array($queue->id, $exsistingQueues)) {
                    $list[$queue->id] = "<" . CHtml::encode($queue->extension) . "> " . CHtml::encode($queue->name);
                }
            } else {
                $list[$queue->id] = "<" . CHtml::encode($queue->extension) . "> " . CHtml::encode($queue->name);
            }
        }
        return $list;
    }

    public function renderTiersType(PhonalisaRecord $data, $row)
    {
        return TiersType::getTiersType($data->type);
    }
}
