/* eslint-disable compat/compat */
// Local
export default async function loginLocal(localUrl, username, password) {
  if (typeof localUrl === 'undefined') {
    return new Error('No localUrl ACS01');
  }
  const fetchData = await fetch(`http://${localUrl}/apiv1/auth/login`, {
    method: 'POST',
    credentials: 'include',
    mode: 'no-cors', // 'cors' by default
    body: JSON.stringify({
      username,
      password,
    }),
  })
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status.toString());
      }
      return response.json();
    })
    .then((json) => {
      // if (debug) console.log("3. <FETCH OK - Login Local> ", json);
      return json;
    })
    .catch((error) => {
      // if (debug) console.log("!! 3. <FETCH FAIL - Login Local> ", error);
      return error;
    });
  return fetchData;
}

export async function openPassage(localUrl, defaultPassageId) {
  if (typeof localUrl === 'undefined') {
    return new Error();
  }
  const fetchData = await fetch(`http://${localUrl}/apiv1/passage/action`, {
    method: 'POST',
    credentials: 'include',
    mode: 'no-cors', // 'cors' by default
    body: JSON.stringify({
      action: 1,
      id: defaultPassageId,
    }),
  })
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status.toString());
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function controlPassage(localUrl, defaultPassageId, action) {
  if (typeof localUrl === 'undefined') {
    return new Error();
  }
  console.log('DATA FOR FETCH', [localUrl, defaultPassageId]);
  const fetchData = await fetch(`http://${localUrl}/apiv1/passage/action`, {
    method: 'POST',
    credentials: 'include',
    mode: 'no-cors', // 'cors' by default
    body: JSON.stringify({
      action,
      id: defaultPassageId,
    }),
  })
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status.toString());
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function printTicket(localUrl, defaultPrinterId) {
  if (typeof localUrl === 'undefined') {
    return new Error();
  }
  const fetchData = await fetch(
    `http://${localUrl}/apiv1/printer/${defaultPrinterId}/print`,
    {
      method: 'POST',
      credentials: 'include',
      mode: 'no-cors', // 'cors' by default
      body: JSON.stringify({}),
    }
  )
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status.toString());
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function onlineTicketById(localUrl, onlineTicketId) {
  if (typeof localUrl === 'undefined') {
    return new Error();
  }
  const fetchData = await fetch(
    `http://${localUrl}/apiv1/ticket/${onlineTicketId}`,
    {
      method: 'GET',
      credentials: 'include',
      mode: 'no-cors', // 'cors' by default
    }
  )
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function onlineTicketByIdChangeState(localUrl, onlineTicketId) {
  if (typeof localUrl === 'undefined') {
    return new Error();
  }
  const fetchData = await fetch(
    `http://${localUrl}/apiv1/ticket/${onlineTicketId}/state`,
    {
      method: 'PUT',
      credentials: 'include',
      mode: 'no-cors', // 'cors' by default
      body: JSON.stringify({
        state: 4,
      }),
    }
  )
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function postBadge(localUrl, codeBadge, defaultReaderId) {
  if (localUrl === '') {
    return new Error();
  }
  if (defaultReaderId === '') {
    return new Error();
  }
  if (codeBadge === '') {
    return new Error();
  }

  const fetchData = await fetch(`http://${localUrl}/apiv1/reader/scan`, {
    method: 'POST',
    credentials: 'include',
    mode: 'no-cors', // 'cors' by default
    body: JSON.stringify({
      code: codeBadge,
      id: defaultReaderId,
      type: 1,
    }),
  })
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function runScenario(localUrl, defaultScenarioId) {
  if (typeof localUrl === 'undefined') {
    return new Error('No default ACS01');
  }
  const fetchData = await fetch(`http://${localUrl}/apiv1/scenario/run`, {
    method: 'POST',
    credentials: 'include',
    mode: 'no-cors', // 'cors' by default
    body: JSON.stringify({
      id: defaultScenarioId,
    }),
  })
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}
