/* eslint-disable compat/compat */
/* eslint-disable radix */
import React, { useState } from 'react';
import { Grid, Typography, Fab } from '@mui/material';
import { observer } from 'mobx-react';
import { ipcRenderer } from 'electron';
import { getRootStore } from '../../models/root-store';

function HomeContentBottomEntry() {
  const [rootStore] = useState(() => getRootStore());

  const selectedCounter = rootStore.counterList.all.find(
    (c) => c.id === rootStore.terminal.counterIdToCheck
  );
  
  const isParkingClosed =
    rootStore.terminal.useParkingCounterLogic &&
    selectedCounter &&
    selectedCounter.value > 0;

  const isPassageOpen = rootStore.passageList.actionState === 1;

  

  const printTicketProcess = async () => {
    if (typeof rootStore.acs01List.defaultAcs01 === 'undefined') return;
    // Authenticate on the Acs01
    if (rootStore.settings.applicationMode === 1) {
      ipcRenderer.postMessage(
        'mainWindow->main->workerWindowNetwork-printTicket',
        [
          rootStore.scenarioList.defaultScenarioConfigured,
          rootStore.scenarioList.defaultScenarioId,
          rootStore.printerList.defaultPrinterId,
        ],
        []
      );
    }
  };

  const printTicketClick = async () => {
    if (typeof rootStore.acs01List.defaultAcs01 === 'undefined') return;
    rootStore.printerList.setNewPrintSequence(true);
    // Authenticate on the Acs01
    if (rootStore.settings.applicationMode === 1) {
      rootStore.printerList.setNewPrintCommandSend(true);
      await printTicketProcess();
    }
  };

  const showButton = () => {
    if (!rootStore.printerList.newPrintSequence) {
      return true;
    }
    return false;
  };

  const getText = () => {
    if (
      typeof rootStore.languageList.t1('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') !== 'undefined' &&
      typeof rootStore.languageList.t3('Ticket') !== 'undefined'
    ) {
      return (
        <>
          <Typography variant="h3" noWrap>
            {rootStore.languageList.t2('Ticket')}
          </Typography>
          <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
            {rootStore.languageList.t3('Ticket')}
          </Typography>
        </>
      );
    }
    if (
      typeof rootStore.languageList.t1('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t3('Ticket') !== 'undefined'
    ) {
      return (
        <Typography variant="h3" noWrap>
          {rootStore.languageList.t3('Ticket')}
        </Typography>
      );
    }
    if (
      typeof rootStore.languageList.t1('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t3('Ticket') === 'undefined'
    ) {
      return (
        <Typography variant="h3" noWrap>
          Please select a language in the settings
        </Typography>
      );
    }
    if (
      typeof rootStore.languageList.t1('Ticket') !== 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t3('Ticket') !== 'undefined'
    ) {
      return (
        <>
          <Typography variant="h3" noWrap>
            {rootStore.languageList.t1('Ticket')}
          </Typography>
          <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
            {rootStore.languageList.t3('Ticket')}
          </Typography>
        </>
      );
    }
    if (
      typeof rootStore.languageList.t1('Ticket') !== 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') !== 'undefined' &&
      typeof rootStore.languageList.t3('Ticket') === 'undefined'
    ) {
      return (
        <>
          <Typography variant="h3" noWrap>
            {rootStore.languageList.t1('Ticket')}
          </Typography>
          <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
            {rootStore.languageList.t2('Ticket')}
          </Typography>
        </>
      );
    }
    if (
      typeof rootStore.languageList.t1('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') !== 'undefined' &&
      typeof rootStore.languageList.t3('Ticket') === 'undefined'
    ) {
      return (
        <Typography variant="h3" noWrap>
          {rootStore.languageList.t2('Ticket')}
        </Typography>
      );
    }
    if (
      typeof rootStore.languageList.t1('Ticket') === 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') !== 'undefined' &&
      typeof rootStore.languageList.t3('Ticket') !== 'undefined'
    ) {
      return (
        <>
          <Typography variant="h3" noWrap>
            {rootStore.languageList.t2('Ticket')}
          </Typography>
          <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
            {rootStore.languageList.t3('Ticket')}
          </Typography>
        </>
      );
    }
    if (
      typeof rootStore.languageList.t1('Ticket') !== 'undefined' &&
      typeof rootStore.languageList.t2('Ticket') === 'undefined' && 
      typeof rootStore.languageList.t3('Ticket') === 'undefined'
    ) {
      return (
        <Typography variant="h3" noWrap>
          {rootStore.languageList.t1('Ticket')}
        </Typography>
      );
    }

    if (
      rootStore.languageList.t1('Ticket') ===
      rootStore.languageList.t2('Ticket')
    ) {
      return (
        <Typography variant="h3" noWrap>
          {rootStore.languageList.t1('Ticket')}
        </Typography>
      );
    }
    if (
      rootStore.languageList.t2('Ticket') ===
      rootStore.languageList.t3('Ticket')
    ) {
      return (
        <>
          <Typography variant="h3" noWrap>
            {rootStore.languageList.t1('Ticket')}
          </Typography>
          <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
            {rootStore.languageList.t2('Ticket')}
          </Typography>
        </>
      );
    }
    if (
      rootStore.languageList.t1('Ticket') ===
      rootStore.languageList.t3('Ticket')
    ) {
      return (
        <>
          <Typography variant="h3" noWrap>
            {rootStore.languageList.t1('Ticket')}
          </Typography>
          <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
            {rootStore.languageList.t2('Ticket')}
          </Typography>
        </>
      );
    }
    return (
      <>
        <Typography variant="h3" noWrap>
          {rootStore.languageList.t1('Ticket')}
        </Typography>
        <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
          {rootStore.languageList.t2('Ticket')}
        </Typography>
        <Typography style={{ marginLeft: 70 }} variant="h3" noWrap>
          {rootStore.languageList.t3('Ticket')}
        </Typography>
      </>
    );
  };

  return (
    <>
        {showButton() && !isParkingClosed && !isPassageOpen ? (         
        <Grid item xs={12}>
          <Grid container justifyContent="center" spacing={0}>
            <Fab
              variant="extended"
              size="large"
              color="primary"
              aria-label="add"
              style={{
                margin: 0,
                paddingLeft: 150,
                paddingRight: 150,
                paddingTop: 50,
                paddingBottom: 50,
              }}
              // className={classes.buttonTicketLost}
              onClick={printTicketClick}
              onTouchStartCapture={printTicketClick}
            >
              {getText()}
            </Fab>
          </Grid>
        </Grid>
      ) : null}
    </>
  );
}

export default observer(HomeContentBottomEntry);
