import { station3Tags } from "./stations/station3.js";
import { wrapperStuff } from "./stations/wrappers.js";

export type TagData = {
    tagHex: string;
    reader: string;
    tag: string;
    timeStamp: Date;
    antenna: number;
    tagStrength: number;
    lastareaIn?: string;
    runningNumber?: number;
};
export const tagData = async (data: TagData[]) => {
    /**
     * We will always update a tag
     */

    if (monitorTagsdata.includes(data[0].tag)) {
    }

    // dyco wall makes sure we have 10 tags
    const station1 = data.some((n) => n.reader.includes("reader1"));

    // by the dyco infeed
    const station2 = data.some((n) => n.reader.includes("station2"));

    // at the end of each line
    const station3 = data.some((n) => n.reader.includes("line3"));

    // at the wrapper
    const wrappers = data.some((n) => n.reader.includes("wrapper"));

    // station checks
    if (station1 && data.length != 10) {
        console.log(`There are only ${data.length}`);
    }

    if (station2) {
        console.log(`There are only ${data.length}`);
    }

    if (station3) {
        station3Tags(data);
    }

    if (wrappers) {
        wrapperStuff(data);
    }
};

const monitorTagsdata: any = [];
const monitorTags = (tag: string) => {
    /**
     * If we have to check more than 10 tags in an hour send an email to alert everyone.
     */
    const now = new Date(Date.now()).getTime();
    monitorTagsdata.push({ timestamp: now, tag: tag });

    // remove if creater than 1 hour
    const removalTiming = now - 2 * 60 * 1000; // 2 min
    while (
        monitorTagsdata > 0 &&
        monitorTagsdata[0].timestamp < removalTiming
    ) {
        monitorTagsdata.shift();
    }
};
