import dayjs from "dayjs";
import advancedFormat from "dayjs/plugin/advancedFormat";
import type { Metadata } from "./Metadata";
import type { HasMetadata } from "./repo/ResourceRepo";
import axios from "axios";

export class Pod implements HasMetadata
{
    constructor (
        public metadata: Metadata,
        public spec: Spec,
        public status: Status
    ) { }

    static getByDeployment(namespace: string, name: string, onSuccess: (pods: Pod[]) => void)
    {
        axios.get<Pod[]>(StringUtils.format('%s/resources/deployments/%s/%s/pods', ApiConfig.getHttpBase(), namespace, name), {
            headers: {
                Authorization: "Bearer " + requireToken()
            }
        })
        .then((response) => {
            onSuccess(response.data)
        });
    }
}

class Spec {
    nodeName?: string;

    constructor (
        public containers: Container[]
    ) { }
}

class Status
{
    phase?: PodStatus

    constructor(
        public containerStatuses: ContainerStatus[]
    ) {}
}

export class Container 
{
    constructor (
        public name: string,
        public image: string
    ) {}
}

export class ContainerStatus
{
    constructor(
        public restartCount: number
    ) {}
}

enum PodStatus 
{
    RUNNING = "Running",
    SUCCEEDED = "Succeeded",
    FAILED = "Failed"
}

enum ChronoUnit 
{
    SECOND, MINUTE, HOUR, DAY
}

export function calcAge(datetime: string | undefined)
{
    dayjs.extend(advancedFormat);
    if(datetime != null)
    {
        const today = Number(dayjs(useSeconds().value).format('X'));
        const createdAt = Number(dayjs(datetime).format('X'));
        const dif = today - createdAt;
        if(dif < 60) 
        {
            return format(dif, ChronoUnit.SECOND);
        }
        if(dif < 60 * 60) 
        {
            const minutes = Math.floor((dif / 60));
            return format(minutes, ChronoUnit.MINUTE);
        }
        if(dif < 60 * 60 * 24) 
        {
            const hours = Math.floor(dif / (60 * 60));
            return format(hours, ChronoUnit.HOUR);
        }
        else 
        {
            const days = Math.floor(dif / (60 * 60 * 24));
            return format(days, ChronoUnit.DAY);
        }
    }
    return "-";
}

function format(count: number, unit: ChronoUnit): string
{
    if(unit === ChronoUnit.SECOND)
    {
        if(count != 1) 
        {
            return count + " Sekunden";
        }
        else 
        {
            return count + " Sekunde";
        }
    }
    if(unit === ChronoUnit.MINUTE)
    {
        if(count != 1) 
        {
            return count + " Minuten";
        }
        else 
        {
            return count + " Minute";
        }
    }
    if(unit === ChronoUnit.HOUR)
    {
        if(count != 1) 
        {
            return count + " Stunden";
        }
        else 
        {
            return count + " Stunde";
        }
    }
    if(unit === ChronoUnit.DAY)
    {
        if(count != 1) 
        {
            return count + " Tage";
        }
        else 
        {
            return count + " Tag";
        }
    }
    return "-";
}