import { Metadata } from "../Metadata";
import { type HasMetadata } from "../repo/ResourceRepo";
import {State} from "~/classes/Threshold";

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

    static cpuUsageFlag(usage: number): State
    {
        if(usage < 20)
        {
            return State.GREEN;
        }
        if(usage < 60)
        {
            return State.ORANGE;
        }
        return State.RED;
    }

    static memoryUsageFlag(usage: number)
    {
        if(usage < 30)
        {
            return State.GREEN;
        }
        if(usage < 75)
        {
            return State.ORANGE;
        }
        return State.RED;
    }

    static diskUsageFlag(usage: number)
    {
        if(usage < 30)
        {
            return State.GREEN;
        }
        if(usage < 70)
        {
            return State.ORANGE;
        }
        return State.RED;
    }

    static isReady(node: Node): boolean | undefined
    {
        const conditions = node.status.conditions;
        if(conditions != undefined)
        {
            for(const condition of conditions)
            {
                if(condition.type === "Ready" && condition.status === "True")
                {
                    return true;
                }
            }
            return false;
        }
        return undefined;
    }
}

class NodeMetrics
{
    absoluteCpuUsage?: number;
    relativeCpuUsage?: number;
    totalCpu?: number;
    absoluteMemory?: number;
    relativeMemory?: number;
    totalMemory?: number;
    runningPods?: number;
    relativeDiskUsage?: number;
    totalDiskSpace?: number;

}

class Spec
{
    constructor (
        public taints?: Taint[]
    ) {}
}

class Status
{
    constructor (
        public nodeInfo: NodeInfo
    ) {}

    conditions?: Condition[]
}

class Condition
{
    type?: string;
    status?: string;
}

class Taint
{
    constructor (
        public key: string,
        public effect: string
    ) {}
}

class NodeInfo
{
    constructor (
        public osImage: string,
        public kubeletVersion: string
    ) {}
}