<?php

class TargetHelper
{

    public $separator = '_';

    public function getTargetList($targetTypes = array())
    {
        $targetList = array();

        foreach ($targetTypes as $targetType) {
            $targetsForType = $this->getTargetsForType($targetType);
            if ($targetsForType) {
                $title = $this->getTranslationForTargetType($targetType);
                $targetList[$title] = $targetsForType;
            }
        }

        return $targetList;
    }

    public function getModelForTargetType($targetType)
    {
        switch ($targetType) {
            case TargetType::User:
            case TargetType::UserVoiceMail:
                return User::model();
            case TargetType::Group:
            case TargetType::GroupVoiceMail:
                return Groups::model();
            case TargetType::Ivr:
                require_once(__DIR__ . '/../modules/routingIVR/models/IVR.php');
                return IVR::model();
            case TargetType::Queue:
                return Queue::model();
            case TargetType::CallGroup:
                return CallGroup::model();
            case TargetType::Conference:
                return Conference::model();
            default:
                return null;
        }
    }

    public function getTargetsForType($type, $searchAttribut = 'extension')
    {
        $model = $this->getModelForTargetType($type);

        // Invalide or unsupported target type
        if ($model == null) {
            return null;
        }

        $modelTargets = $model->find(array(), $searchAttribut);

        // No targets in model found
        if ($modelTargets == null) {
            return null;
        }

        $targetList = array();
        foreach ($modelTargets as $target) {

            if ($target->extension !== '') {    // becouse we just need Targets with extension
                // FIXME
                // use $this->getTitlePatternForTargetType() as value...
                $title = $this->getTitleForModelAndTargetType($target, $type);
                if ($type == TargetType::UserVoiceMail || $type == TargetType::GroupVoiceMail) {
                    $targetList[(int) $target->extension][$type . $this->separator . $target->id] = $title . __('VOICE_MAIL_SUFFIX');
                } else {
                    $targetList[(int) $target->extension][$type . $this->separator . $target->id] = $title;
                }
            }
        }

        $targetList = $this->sortTargetList($targetList);
        if (empty($targetList)) {
            return null;
        }

        return $targetList;
    }

    public function sortTargetList($listData)
    {
        $sortedListData = array();
        ksort($listData);
        foreach ($listData as $data) {
            foreach ($data as $key => $value) {

                $sortedListData[$key] = $value;
            }
        }

        return $sortedListData;
    }

    public function getTranslationForTargetType($targetType)
    {
        switch ($targetType) {
            case TargetType::User:
                return __('USERS');
            case TargetType::UserVoiceMail:
                return __('USER_VOICE_MAILS');
            case TargetType::Group:
                return __('GROUPS');
            case TargetType::GroupVoiceMail:
                return __('GROUP_VOICE_MAILS');
            case TargetType::Ivr:
                return __('IVRS');
            case TargetType::Queue:
                return __('QUEUES');
            case TargetType::CallGroup:
                return __('CALL_GROUP');
            case TargetType::CallThrough:
                return __('ROUTINGINBOUND_MODEL_TARGET_OTHER_CALL_TROUGH');
            case TargetType::UserFax:
                return __('ROUTINGINBOUND_MODEL_TARGET_USER_FAX');
            case TargetType::Conference:
                return __('RENDER_TARGET_CONFERENCE');
            case TargetType::Manual:
                return __('RENDER_TARGET_TYPE_MANUAL');
            case TargetType::CentralDropTarget:
                return __("SYSTEM_GLOBAL_CONFIG_KEY_DP_DROPTARGET_SHORT"); // FIXME ?
            case TargetType::Announcement:
                return __("CONFERENCE_MODEL_ANNOUNCE_FILE"); // FIXME ?
            case TargetType::Extension:
                return __('ROUTING_INBOUND_PATTERN_TO_EXTENSION') . " " . RoutingInbound::TARGET_PATTERN_EXTENSION; // FIXME ?
            default:
                return 'n/a';
        }
    }

    public function getTitleForModelAndTargetType($model, $targetType)
    {
        switch ($targetType) {
            case TargetType::User:
            case TargetType::UserVoiceMail:
                return getFormatedUsername($model);
            case TargetType::Group:
            case TargetType::GroupVoiceMail:
            case TargetType::CallGroup:
                return '<' . $model->extension . '> ' . $model->name;
            case TargetType::Ivr:
                return '<' . $model->extension . '> ' . $model->title;
            case TargetType::Queue:
                return '<' . $model->extension . '> ' . $model->name;
            default:
                return 'n/a';
        }
    }

    public function getSelectedValueForAttributes($typeAttribute, $targetAttribute, $model)
    {
        return $model->$typeAttribute . $this->separator . $model->$targetAttribute;
    }

    public static function setTargetAttributes($typeAttribute, $targetAttribute, &$model, $separator = '_')
    {
        $attributes = explode($separator, $model->$targetAttribute);
        $model->$typeAttribute = $attributes[0];
        $model->$targetAttribute = $attributes[1];
    }

    public function getRedirectionTargetList($withManual = true)
    {
        $annouceFileList = array(
            $this->getTranslationForTargetType(TargetType::Announcement) => $this->getAnnouncementFileList()
        );

        $targetList = array(
            TargetType::User,
            TargetType::UserVoiceMail,
            TargetType::Group,
            TargetType::Ivr,
            TargetType::CallGroup,
            TargetType::Queue
        );

        if ($withManual) {
            $targetList[] = TargetType::Manual;
        }
        return $annouceFileList + $this->getTargetList($targetList);
    }

    private function getAnnouncementFileList()
    {
        $list = Queue::getAnnouncementFileList(false);
        $result = array();
        foreach ($list as $key => $value) {
            $result[TargetType::Announcement . '_' . $key] = $value;
        }
        return $result;
    }

    public static function emptyArrayEntry()
    {
        return array('' => '---');
    }
}
