import { useSelector } from 'react-redux';
import {
  apiCreate,
  apiAccept,
  apiClose,
  apiReject,
  apiStartTrip,
  apiEnd,
  apiPrintEwaybill,
  apiCreatorApprover,
  apiCancel,
  apiExportReport,
  apiUpdate,
  apiArrivedLoading,
  apiStartLoading,
  apiFinishDischarging,
  apiArrivedDischarging,
  apiStartDischarging,
  apiFinishLoading,
  apiStartDischargingTrip,
  apiGateIn,
  apiGateOut,
  apiExportReportIgates
} from '@/services/EwaybillService';

function useEwaybill() {
  const { token, signedIn } = useSelector((state) => state.auth.session);

  const create = async (values) => {
    try {
      const resp = await apiCreate(values);

      if (resp.data) {
        // const redirectUrl = query.get('/ewaybill/grid');
        // console.log(REDIRECT_URL_KEY);
        // navigate(redirectUrl ? redirectUrl : appConfig.authenticatedEntryPath);
        //navigate('/ewaybill/grid');
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      if (errors?.response?.data?.errorCodes) {
        if (errors?.response?.data?.errorCodes[0]?.statusCode === 708) {
          return {
            status: 'failed',
            message: errors?.response?.data?.errorCodes[0]?.errorMessages,
          };
        }
      }
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const accept = async (values) => {
    try {
      const resp = await apiAccept(values);

      if (resp.data) {
        // const redirectUrl = query.get('/ewaybill/grid');
        // navigate(redirectUrl ? redirectUrl : appConfig.authenticatedEntryPath);
        //navigate('/ewaybill/grid');
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const reject = async (values) => {
    try {
      const resp = await apiReject(values);
      if (resp.data) {
        // const redirectUrl = query.get('/ewaybill/grid');
        // navigate(redirectUrl ? redirectUrl : appConfig.authenticatedEntryPath);
        //navigate('/ewaybill/grid');
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const close = async (values) => {
    try {
      const resp = await apiClose(values);
      if (resp.data) {
        // const redirectUrl = query.get('/ewaybill/grid');
        // navigate(redirectUrl ? redirectUrl : appConfig.authenticatedEntryPath);
        //navigate('/ewaybill/grid');
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      if (errors?.response?.data?.errorCodes[0]?.errorMessages) {
        return {
          status: 'warning',
          message: errors?.response?.data?.errorCodes[0]?.errorMessages,
        };
      }

      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const update = async (values, newAttachments, deletedAttachmentNames) => {
    try {
      const resp = await apiUpdate(values, newAttachments, deletedAttachmentNames);
      if (resp.data) {
        // const redirectUrl = query.get('/ewaybill/grid');
        // navigate(redirectUrl ? redirectUrl : appConfig.authenticatedEntryPath);
        //navigate('/ewaybill/grid');
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const startTrip = async (values) => {
    try {
      const resp = await apiStartTrip(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const arrivedLoading = async (values) => {
    try {
      const resp = await apiArrivedLoading(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const startLoading = async (values) => {
    try {
      const resp = await apiStartLoading(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };
  const finishLoading = async (values) => {
    try {
      const resp = await apiFinishLoading(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      console.log(errors);
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const startDischargingTrip = async (values) => {
    try {
      const resp = await apiStartDischargingTrip(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      console.log(errors);
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const arrivedDischarging = async (values) => {
    try {
      const resp = await apiArrivedDischarging(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      console.log(errors);
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const startDischarging = async (values) => {
    try {
      const resp = await apiStartDischarging(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      console.log(errors);
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const finishDischarging = async (values) => {
    try {
      const resp = await apiFinishDischarging(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      console.log(errors);
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const end = async (values) => {
    try {
      const resp = await apiEnd(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const cancel = async (values) => {
    try {
      const resp = await apiCancel(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const print = async (id, locale) => {
    try {
      const resp = await apiPrintEwaybill(id, locale);

      if (resp.data) {
        return {
          status: 'success',
          message: '',
          data: resp.data,
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const exportReport = async (filter, locale) => {
    try {
      const resp = await apiExportReport(filter, locale);

      if (resp.data) {
        return {
          status: 'success',
          message: '',
          data: resp.data,
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const creatorApprover = async (values) => {
    try {
      const resp = await apiCreatorApprover(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
      };
    }
  };

  const gateIn = async (values) => {
    try {
      const resp = await apiGateIn(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
          data: resp.data,
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
        data: null,
      };
    }
  };

  const gateOut = async (values) => {
    try {
      const resp = await apiGateOut(values);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
          data: resp.data,
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
        data: null,
      };
    }
  };

  const exportReportIgates = async (filter, locale) => {
    try {
      const resp = await apiExportReportIgates(filter, locale);
      if (resp.data) {
        return {
          status: 'success',
          message: '',
          data: resp.data,
        };
      }
    } catch (errors) {
      return {
        status: 'failed',
        message: errors?.response?.data?.message || errors.toString(),
        data: null,
      };
    }
  };

  return {
    authenticated: token && signedIn,
    create,
    accept,
    reject,
    close,
    startTrip,
    end,
    print,
    cancel,
    exportReport,
    creatorApprover,
    update,
    arrivedLoading,
    startLoading,
    finishLoading,
    startDischargingTrip,
    arrivedDischarging,
    startDischarging,
    finishDischarging,
    gateIn,
    gateOut,
    exportReportIgates,
  };
}

export default useEwaybill;
