<?php 

namespace common\modules\support\models\redmine;

use common\models\Settings;

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

	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];
    }
}