<?php

use Illuminate\Support\Str;

/*
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.  Please see LICENSE.txt at the top level of
 * the source code distribution for details.
 * @author     LukeKelsall <l.kelsall@b4rn.org.uk>
 */

//loop through ports
foreach ($port_stats as $index => $port) {
    //Logically convert swp ports to human readable
    //only convert swp ports
    if (isset($port['ifName']) && str_starts_with($port['ifName'], 'swp')) {
        $portNum = preg_replace('/[a-z]+/', '', $port['ifName']);
        if ($portNum < 50) {
            $portNum++;
            //Leading 0 for single digits
            $ifName = '1/' . Str::padLeft($portNum, 2, '0');
        }
        if ($portNum > 63 && $portNum < 128) {
            $portNum -= 63;
            //Leading 0 for single digits
            $ifName = '2/' . Str::padLeft($portNum, 2, '0');
        }
        if ($portNum > 127 && $portNum < 192) {
            $portNum -= 127;
            //Leading 0 for single digits
            $ifName = '3/' . Str::padLeft($portNum, 2, '0');
        }
        if ($portNum > 191) {
            $portNum -= 191;
            //Leading 0 for single digits
            $ifName = '4/' . Str::padLeft($portNum, 2, '0');
        }
    } else {
        //Set port number to match current
        $ifName = $port['ifName'] ?? null;
    }
    //set the libre ifName value to new port name
    $port_stats[$index]['ifName'] = $ifName;
}
