import { createLog } from "../../../../logger/logger.js";
import { query } from "../../../../sqlServer/prodSqlServer.js";

export const dualPrintingProcess = async (lotInfo: any) => {
    // currently we will only allow for 2 printers to be linked only crazy people want more than 2
    createLog(
        "info",
        "ocp",
        "ocp",
        "Dual printing running logic here to see what to print"
    );
    // each check
    let lastPrinted;
    let firstLine;
    let secondLine;

    // this query checks the last line to print a label
    const printedQuery = `SELECT TOP(1) IdProdPlanung, 
    IdArtikelvarianten, 
    IdMaschine,
    LfdNr, 
    ArtikelVariantenBez, 
    Add_User, 
    Add_Date, 
    Upd_User, 
    Upd_Date, 
    IdProdBereich 
    FROM AlplaPROD_test1.dbo.T_EtikettenGedruckt (nolock) 
    where IdMaschine in (${lotInfo[0].machineID},${lotInfo[1].machineID}) order by Add_Date desc`;

    const fisrtLineDT = `SELECT top(1) IdMaschine, 
    case when Endzeit ='1900-01-01 00:00:00.000' and DATEDIFF(MINUTE, Startzeit, GETDATE()) >= 3 Then 1 else 0 end as 'LineCheck' 
    FROM AlplaPROD_test1.dbo.T_HistoryStillstandsereignis (nolock) where  IdMaschine = ${lotInfo[0].machineID} order by Add_Date desc`;

    const secondLineDT = `SELECT top(1) IdMaschine, 
    case when Endzeit ='1900-01-01 00:00:00.000' and DATEDIFF(MINUTE, Startzeit, GETDATE()) >= 3 Then 1 else 0 end as 'LineCheck' 
    FROM AlplaPROD_test1.dbo.T_HistoryStillstandsereignis (nolock) where  IdMaschine = ${lotInfo[1].machineID} order by Add_Date desc`;

    try {
        // get what was last printed
        const result: any = await query(printedQuery, "Last Printed Query");
        lastPrinted = result.data[0].IdMaschine;
    } catch (err) {
        createLog(
            "error",
            "ocp",
            "ocp",
            `Error: getting last printed querty: ${err}`
        );
    }

    try {
        // get if if running or down
        const first: any = await query(fisrtLineDT, "First line DT Check");
        firstLine = first.data[0].LineCheck;
    } catch (err) {
        createLog(
            "error",
            "ocp",
            "ocp",
            `Error: with getting ${lotInfo[0].machineID} data query: ${err}`
        );
    }

    try {
        const second: any = await query(secondLineDT, "Second line DT Check");
        secondLine = second.data[0].LineCheck;
    } catch (err) {
        createLog(
            "error",
            "ocp",
            "ocp",
            `Error: with getting ${lotInfo[1].machineID} data query: ${err}`
        );
    }

    // first check if both are up and running
    if (firstLine === 0 && secondLine === 0) {
        createLog("info", "ocp", "ocp", "Both lines are up");
        // what line last printed
        if (lastPrinted === lotInfo[0].machineID) {
            // IE 7 then print 8, usslc was the main example for all of this
            let whatToPrint = lotInfo.filter(
                (m: any) => m.machineID === lotInfo[1].machineID
            );
            // send to label
            createLog(
                "info",
                "ocp",
                "ocp",
                `Printing label for ${whatToPrint[0].MachineDescription}`
            );
            return whatToPrint;
        }

        if (lastPrinted === lotInfo[1].machineID) {
            // IE 8 then print 7, usslc was the main example for all of this
            let whatToPrint = lotInfo.filter(
                (m: any) => m.machineID === lotInfo[0].machineID
            );
            // send to label
            createLog(
                "info",
                "ocp",
                "ocp",
                `Printing label for ${whatToPrint[0].MachineDescription}`
            );
            return whatToPrint;
        }
    }

    // as we are only allowing 2 lines if one is down the other must be up... still going to conditional it to be sure
    createLog(
        "info",
        "ocp",
        "ocp",
        "Looking at what line is down and printing the other one!"
    );
    if (firstLine === 1 && secondLine === 0) {
        // print the other line
        let whatToPrint = lotInfo.filter(
            (m: any) => m.machineID === lotInfo[1].machineID
        );

        // send to label
        createLog(
            "info",
            "ocp",
            "ocp",
            `Printing label for ${whatToPrint.MachineDescription}`
        );
        return whatToPrint;
    } else if (firstLine === 0 && secondLine === 1) {
        // print the other line
        let whatToPrint = lotInfo.filter(
            (m: any) => m.machineID === lotInfo[0].machineID
        );
        // send to label
        createLog(
            "info",
            "ocp",
            "ocp",
            `Printing label for ${whatToPrint.MachineDescription}`
        );
        return whatToPrint;
    }
};
