import { atom, useAtom } from 'jotai';
import {
  CreateReminderPayload,
  CreateTaskPayload,
  TaskUpdateType,
  UpdateReminderPayload,
  allTypes,
  resolveTaskPayload,
  splitOrderType,
} from './types';
import { OrdersType } from '../types';
import { ordersInitialValue } from '../store';

export const initialPayload: CreateTaskPayload = {
  description: '',
  is_private: true,
  order_id: null,
  flag_type: '',
  priority: 3,
  status: 'AD',
  due_datetime: '',
};

export const initialValue: CreateReminderPayload = {
  due_datetime: null,
  description: '',
  assigned_to: '',
  order_id: null,
  priority: 3,
  department: '',
  status: false,
};

export const updatePayload: UpdateReminderPayload = {
  due_datetime: '',
  description: '',
  assigned_to: '',
  order_id: null,
  priority: null,
  department: null,
  status: false,
  id: null,
  is_deleted: null,
  legacy_reminder_id: null,
  order_data: null,
  reference: null,
  completed_at: null,
  completed_by: null,
};

export const flagInitialValue: TaskUpdateType = {
  description: '',
  flag_type: 0,
  priority: 0,
  id: null,
  is_private: false,
};

export const resolvedValue: resolveTaskPayload = {
  task_ids: null,
};

const allStaticData = atom<allTypes[] | null>([]);

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

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

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

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

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

const createTaskPayload = atom<CreateTaskPayload>(initialPayload);

const createReminderPayload = atom<CreateReminderPayload>(initialValue);

const reminderUpdatePayload = atom<UpdateReminderPayload>(updatePayload);

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

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

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

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

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

const flagRefId = atom<number | null>(null);

const ReminderRefId = atom<number | null>(null);

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

const resolvedModalPayload = atom<resolveTaskPayload>(resolvedValue);

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

const orderNotifyCount = atom<number>(0);

//errors
const errorMessage = atom<any>({
  taskType: '',
  description: '',
  dueDate: '',
});

const defaultLoadingText = {
  title: '',
  subtitle: '',
};

const loadingText = atom<{ title: string; subtitle: string } | undefined>(
  defaultLoadingText
);

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

const loading = atom<boolean>(true);

export function useFlagsLoading() {
  return useAtom(loading);
}

const taskLoading = atom<boolean>(true);

export function useTaskLoading() {
  return useAtom(taskLoading);
}

export const initialConfirmOrderErrValues = {
  homeContactError: '',
  workContactError: '',
  mobileError: '',
  emailError: '',
};
export const splitOrderinitialValue: splitOrderType = {
  return_order: 0,
  products: [],
  force: false,
};

const errorConfirmOrderAtom = atom(initialConfirmOrderErrValues);

export function useErrorConfirmOrder() {
  return useAtom(errorConfirmOrderAtom);
}

//confirm order
const confirmOrderDate = atom<any>(null);

export function useConfirmOrderDate() {
  return useAtom(confirmOrderDate);
}

const OrderDetailsConfirmOrder = atom<OrdersType>(ordersInitialValue);
export function useOrderDetailsConfirmOrder() {
  return useAtom(OrderDetailsConfirmOrder);
}

const splitOrderPayload = atom<splitOrderType>(splitOrderinitialValue);

export function useSplitOrderPayload() {
  return useAtom(splitOrderPayload);
}
const selectedSplitList = atom<any[]>([]);

export function useSelectedSplit() {
  return useAtom(selectedSplitList);
}

export function useLoadingText() {
  return useAtom(loadingText);
}

export function useCreateTaskPayload() {
  return useAtom(createTaskPayload);
}

export function useCreateReminderPayload() {
  return useAtom(createReminderPayload);
}

export function useStaticData() {
  return useAtom(allStaticData);
}

export function useTaskPriority() {
  return useAtom(taskPriorityData);
}
export function useCategoryData() {
  return useAtom(taskCategoryData);
}

export function useDepartmentData() {
  return useAtom(departmentData);
}

export function useNotiifcationLabel() {
  return useAtom(notificationLabel);
}

export function useTaskTypeData() {
  return useAtom(taskTypeData);
}
export function useErrorMessage() {
  return useAtom(errorMessage);
}
export function useTaskTypeExtended() {
  return useAtom(taskTypeExtended);
}

export function useTasksData() {
  return useAtom(notificationData);
}

export function useDeptData() {
  return useAtom(deptData);
}

export function useReminderNotiData() {
  return useAtom(reminderNotifiData);
}

export function useFlagById() {
  return useAtom(flagByIdData);
}

const taskUpdate = atom<TaskUpdateType>(flagInitialValue);

export function useUpdateTask() {
  return useAtom(taskUpdate);
}

export function useSingleFlagData() {
  return useAtom(singleFlagData);
}

export function useFlagRefId() {
  return useAtom(flagRefId);
}

export function useResolvedPayload() {
  return useAtom(resolvedModalPayload);
}

export function useReminderRefId() {
  return useAtom(ReminderRefId);
}

export function useReminderById() {
  return useAtom(reminderData);
}

export function useUpdateReminderData() {
  return useAtom(reminderUpdatePayload);
}

export function useRouteDetails() {
  return useAtom(routeDetails);
}
const allOrderStatus = atom<string | null>(null);

export function useAllOrderStatus() {
  return useAtom(allOrderStatus);
}

export const selectedCategoryIdAtom = atom<any>(null);
export const selectedDocumentTypeAtom = atom<any>(null);
export const selectedTypeAtom = atom<any>(null);
export const selectedVisibilityAtom = atom<any>(null);
export const setResourcesCall = atom<boolean>(false);

export function useOrderNotify() {
  return useAtom(orderNotifyCount);
}
