import { atom, useAtom } from 'jotai';
import {
  AssignUpdatePayload,
  CreateReturnPayload,
  SelectDateType,
} from './types';
import dayjs from 'dayjs';

export const filtersInitialValue = {
  date: {
    startDate: undefined,
    endDate: undefined,
  },
};

export const CreateReturnInitial = {
  customer_id: 0,
  account_number: '',
  return_location_token: '',
  products: [],
  return_action: [],
};

export const AssignUpdateInitial = {
  return_order_id: 0,
  bjs_delivery: false,
};

export const triggerApiCallAtom = atom(false);

const staticData = atom<any>({});
const filterAtom = atom<SelectDateType>(filtersInitialValue);

const rowCountDeliveredAtom = atom<number>(5);

const rowCountCollectedAtom = atom<number>(5);

const reminderTableData = atom<any[]>([]);

const inQueueDataAtom = atom<any>({});

const customerData = atom<any>([]);

const actionTypeData = atom<any>([]);

const locationData = atom<any>([]);

const createReturnPayload = atom<CreateReturnPayload>(CreateReturnInitial);

const newPayload = atom<any>([]);

const grpPayload = atom<any>([]);

const statusOfOrder = atom<any>([]);

const selectedLocationConfirm = atom<any[]>([]);

const selectAll = atom<boolean>(false);

const postLoading = atom<boolean>(false);

const final = atom<any[]>([]);

const clearState = atom<boolean>(false);

const assignData = atom<any[]>([]);

const visibilityData = atom<any[]>([]);

const assignPayload = atom<AssignUpdatePayload>(AssignUpdateInitial);

const callAPIAgain = atom<boolean>(true);

const actionTypeOptions = atom<any[]>([]);

const locationOptions = atom<any[]>([]);

const selectedProductsMove = atom<any>([]);

const newReturnOrderData = atom<any>([]);

const subContracterData = atom<any>([]);

export function useSubContracterData() {
  return useAtom(subContracterData);
}

function useReminderData() {
  return useAtom(reminderTableData);
}

function useRowDeliveredCount() {
  return useAtom(rowCountDeliveredAtom);
}

function useRowCollectedCount() {
  return useAtom(rowCountCollectedAtom);
}

function useFilter() {
  return useAtom(filterAtom);
}
function useStaticData() {
  return useAtom(staticData);
}
function useInQueueData() {
  return useAtom(inQueueDataAtom);
}

const emailInvoiceData = atom<any>({});

function useEmailInvoice() {
  return useAtom(emailInvoiceData);
}

const selectedOrderlList = atom<any[]>([]);

function useSelectedOrders() {
  return useAtom(selectedOrderlList);
}

const selectedCPOrderlList = atom<any[]>([]);

function useSelectedClientPortOrders() {
  return useAtom(selectedCPOrderlList);
}

const selectedCPOrderAfterlList = atom<any[]>([]);

function useSelectedClientAfterOrders() {
  return useAtom(selectedCPOrderAfterlList);
}

//start date and end date
const startCPDate = atom<dayjs.Dayjs | null>(dayjs());

function useStartDate() {
  return useAtom(startCPDate);
}

const endCPDate = atom<dayjs.Dayjs | null>(null);

function useEndDate() {
  return useAtom(endCPDate);
}

// Sticky menu state
const stickyStatus = atom<boolean>(false);
export const useStickyStatus = () => {
  return useAtom(stickyStatus);
};

//search for pending
// Sticky menu state
const search = atom<any>('');
export const useSearchText = () => {
  return useAtom(search);
};

const searchAfter = atom<any>('');
export const useSearchAfterText = () => {
  return useAtom(searchAfter);
};

const rclientReturnDataAtom = atom<any[]>([]);
export function useClientOutstandingReturnData() {
  return useAtom(rclientReturnDataAtom);
}

const returnConfirmedOrderAtom = atom<any[]>([]);

export function useReturnConfirmedOrderData() {
  return useAtom(returnConfirmedOrderAtom);
}
const statsCPCount = atom<any>(null);

export function useStatsCPCount() {
  return useAtom(statsCPCount);
}

function useCustomerData() {
  return useAtom(customerData);
}

function useActionType() {
  return useAtom(actionTypeData);
}

function useLocationData() {
  return useAtom(locationData);
}

function useReturnPayload() {
  return useAtom(createReturnPayload);
}

function useNewReturnPayload() {
  return useAtom(newPayload);
}

function useGroupedPayload() {
  return useAtom(grpPayload);
}

function useStatusOrderData() {
  return useAtom(statusOfOrder);
}

function useSelectedConfirmLoc() {
  return useAtom(selectedLocationConfirm);
}

function useSelectAll() {
  return useAtom(selectAll);
}

function usePostLoading() {
  return useAtom(postLoading);
}

function useFinalPayload() {
  return useAtom(final);
}

function useClearState() {
  return useAtom(clearState);
}

function useAssignData() {
  return useAtom(assignData);
}

function useVisibilityData() {
  return useAtom(visibilityData);
}

function useAssignPayload() {
  return useAtom(assignPayload);
}

function useCallApiAgain() {
  return useAtom(callAPIAgain);
}

function useActionTypeOptions() {
  return useAtom(actionTypeOptions);
}

function useLocationOptions() {
  return useAtom(locationOptions);
}
function useSelectedProducts() {
  return useAtom(selectedProductsMove);
}

const selectedAssignee = atom<any>('');

export function useSelectedData() {
  return useAtom(selectedAssignee);
}

export function useNewReturnOrderData() {
  return useAtom(newReturnOrderData);
}

const isClientPortalAtom = atom<boolean>(false);
function useClientPortal() {
  return useAtom(isClientPortalAtom);
}
const activeCardIdAtom = atom<number>(1);
export const useActiveCardId = () => {
  return useAtom(activeCardIdAtom);
};
export {
  useAtom,
  useFilter,
  useStaticData,
  useRowCollectedCount,
  useRowDeliveredCount,
  useReminderData,
  useInQueueData,
  useEmailInvoice,
  // useSelectedEmail,
  useCustomerData,
  useActionType,
  useLocationData,
  useSelectedOrders,
  useReturnPayload,
  useNewReturnPayload,
  useGroupedPayload,
  useStatusOrderData,
  useSelectedConfirmLoc,
  useSelectAll,
  usePostLoading,
  useFinalPayload,
  useClearState,
  useAssignData,
  useAssignPayload,
  useCallApiAgain,
  useActionTypeOptions,
  useLocationOptions,
  useSelectedProducts,
  useVisibilityData,
  useClientPortal,
  useSelectedClientPortOrders,
  useStartDate,
  useEndDate,
  useSelectedClientAfterOrders,
};
