<?php 

namespace common\modules\support\models\redmine;

use common\models\Settings;

class RedmineHelper 
{
    const MAX_FILE_SIZE = 5242880; //5mb

    public static $markupSet = [    
        [
            'name' => 'Жирный',
            'className' => 'fa fa-bold',
            'openWith' => '*',
            'closeWith' => '*'
        ],
        [
            'name' => 'Курсив',
            'className' => 'fa fa-italic',
            'openWith' => '_',
            'closeWith' => '_'
        ],
        [
            'name' => 'Подчеркнутый',
            'className' => 'fa fa-underline',
            'openWith' => '+',
            'closeWith' => '+'
        ],
        [
            'name' => 'Зачеркнутый',
            'className' => 'fa fa-strikethrough',
            'openWith' => '-',
            'closeWith' => '-'
        ],
        ['separator' => '---------------'],
        [
            'name' => 'Заголовок 1',
            'className' => 'fa fa-header header-1',
            'openWith' => 'h1. ',
            'closeWith' => ''
        ],
        [
            'name' => 'Заголовок 2',
            'className' => 'fa fa-header header-2',
            'openWith' => 'h2. ',
            'closeWith' => ''
        ],
        [
            'name' => 'Заголовок 3',
            'className' => 'fa fa-header header-3',
            'openWith' => 'h3. ',
            'closeWith' => ''
        ],
        ['separator' => '---------------'],
        [
            'name' => 'Маркированный список',
            'className' => 'fa fa-list-ul',
            'openWith' => '* ',
            'multiline' => true,
            'openBlockWith' => "",
            'closeBlockWith' => ""
        ],
        [
            'name' => 'Нумерованный список',
            'className' => 'fa fa-list-ol',
            'openWith' => '# ',
            'multiline' => true,
            'openBlockWith' => "",
            'closeBlockWith' => ""
        ],
        ['separator' => '---------------'],
        [
            'name' => 'Заранее форматированный тест',
            'className' => 'fa fa-newspaper-o',
            'openWith' => '<pre>',
            'closeWith' => '</pre>'
        ],
        ['separator' => '---------------'],
        [
            'name' => 'Вставка изображения',
            'className' => 'fa fa-picture-o',
            'openWith' => '!',
            'closeWith' => '!'
        ],
    ];

	public static function sortIsuues($issues = null)
	{
        if($issues)
        {
    		$settings = [
                [
                    'statuses' => explode(',', Settings::getValue('support-status-work')),
                    'title' => 'Задачи в работе',
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-new')),
                    'title' => 'Задачи на оценке у менеджера',
                    'rating' => true,
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-rework')),
                    'title' => 'Задачи на доработке',
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-approve')),
                    'title' => 'Задачи ожидающие проверки',
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-accepted')),
                    'title' => 'Закрытые задачи',
                    'close' => true
                ],
            ];

            foreach ($issues as $issue) 
            {
                foreach ($settings as &$setting) 
                {
                    if(in_array($issue['status']['id'], $setting['statuses']))
                    {
                        $setting['models'][] = $issue;
                    }
                }
            }

            unset($setting);

            return $settings;
        }

        return $issues;
	}

    public static function sortMemberships($memberships = null)
    {
        if($memberships)
        {
            $output = [];
            foreach ($memberships as $membership) 
            {
                if(isset($membership['roles']))
                {
                    foreach ($membership['roles'] as $role) 
                    {
                        if($role['id'] == 5)
                        {
                            $output[$membership['project']['id']] = $membership['project']['name'];
                        }
                    }
                }
            }

            return $output;
        }

        return $memberships;
    }

    public static function formatSize($value)
    {
        $units=array('B','KB','MB','GB','TB');
        $base = 1024; 

        for($i = 0; $base <= $value; $i++) 
        {
            $value=$value/$base;
        }

        return round($value, 2) . ' ' . $units[$i];
    }
}