import { PropsWithChildren } from "react";
import UserProvider, { useUserAPI } from "./userAPI";
import TeamProvider, { useTeamAPI } from "./teamAPI";
import { useImagekitAPI } from "./imagekitAPI";
import PermissionProvider, { usePermissionAPI } from "./permissionAPI";
import { Scope } from "models";
import BinProvider, { useBinAPI } from "./binAPI";
import GateProvider, { useGateAPI } from "./gateAPI";
import NoteProvider, { useNoteAPI } from "./noteAPI";
import DeviceProvider, { useDeviceAPI } from "./deviceAPI";
import BackpackProvider, { useBackpackAPI } from "./backpackAPI";
import GroupProvider, { useGroupAPI } from "./groupAPI";
import NotificationProvider, { useNotificationAPI } from "./notificationAPI";
import TagProvider, { useTagAPI } from "./tagAPI";
import FirmwareProvider, { useFirmwareAPI } from "./firmwareAPI";
import ComponentProvider, { useComponentAPI } from "./componentAPI";
import InteractionProvider, { useInteractionsAPI } from "./interactionsAPI";
import BinYardProvider, { useBinYardAPI } from "./binYardAPI";
import GrainBagProvider, { useGrainBagAPI } from "./grainBagAPI";
import FieldProvider, { useFieldAPI } from "./fieldAPI";
import TransactionProvider, { useTransactionAPI } from "./transactionAPI";
import FileControllerProvider, { useFileControllerAPI } from "./fileControllerAPI";
import MineProvider, { useMineAPI } from "./mineAPI";
import FieldMarkerProvider, {useFieldMarkerAPI} from "./fieldMarkerAPI";
import HomeMarkerProvider, {useHomeMarkerAPI} from "./homeMarkerAPI";
import HarvestPlanProvider, {useHarvestPlanAPI} from "./harvestPlanAPI";
import TerminalProvider, {useTerminalAPI} from "./terminalAPI"
import SiteProvider, {useSiteAPI} from "./siteAPI";
import ObjectHeaterProvider, {useObjectHeaterAPI} from "./ObjectHeaterAPI";
import MutationProvider, {useMutationAPI} from "./mutationAPI"
import PreferenceProvider, {usePreferenceAPI} from "./preferenceAPI";
import TaskProvider, { useTaskAPI } from "./taskAPI";
import DataDogProvider, { useDataDogProxyAPI } from "./datadogProxyAPI";
import ContractProvider, { useContractAPI } from "./contractAPI";
import UsageProvider, { useUsageAPI } from "./usageAPI";
import KeyManagerProvider, { useKeyManagerAPI } from "./keyManagerAPI";
import JohnDeereProvider, { useJohnDeereProxyAPI } from "./johnDeereProxyAPI";
import LibraCartProvider, { useLibraCartProxyAPI } from "./libracartProxyAPI";
import CNHiProvider, { useCNHiProxyAPI } from "./cnhiProxyAPI";
import DevicePresetProvider, { useDevicePresetAPI } from "./devicePresetAPI";
// import NoteProvider from "providers/noteAPI";

export const pondURL = (partial: string, demo: boolean = false): string => {
  let url = import.meta.env.VITE_APP_API_URL + (demo ? "/demo" : "") + partial;
  // console.log("API URL: "+url)
  // if (isStaging()) url = process.env.REACT_APP_STAGING_API_URL + (demo ? "/demo" : "") + partial;
  return url;
};

export const objectQueryParams = (scope: Scope) => {
  return "?" + scope.kind + "_id=" + scope.key;
};

export default function PondProvider(props: PropsWithChildren<any>) {
  const { children } = props;

  return (
    <UserProvider>
      <TeamProvider>
        <PermissionProvider>
          <BinProvider>
            <GateProvider>
              <NoteProvider>
                <DeviceProvider>
                  <GroupProvider>
                    <BackpackProvider>
                      <NotificationProvider>
                        <TagProvider>
                          <FirmwareProvider>
                            <ComponentProvider>
                              <InteractionProvider>
                                <BinYardProvider>
                                  <GrainBagProvider>
                                    <FieldProvider>
                                      <TransactionProvider>
                                        <FileControllerProvider>
                                          <MineProvider>
                                            <FieldMarkerProvider>
                                              <HomeMarkerProvider>
                                                <HarvestPlanProvider>
                                                  <TerminalProvider>
                                                    <SiteProvider>
                                                      <ObjectHeaterProvider>
                                                        <MutationProvider>
                                                          <PreferenceProvider>
                                                            <TaskProvider>
                                                              <DataDogProvider>
                                                                <ContractProvider>
                                                                  <JohnDeereProvider>
                                                                    <CNHiProvider>
                                                                      <LibraCartProvider>
                                                                        <UsageProvider>
                                                                          <KeyManagerProvider>
                                                                            <DevicePresetProvider>
                                                                              {children}
                                                                            </DevicePresetProvider>
                                                                          </KeyManagerProvider>
                                                                        </UsageProvider>
                                                                      </LibraCartProvider>
                                                                    </CNHiProvider>
                                                                  </JohnDeereProvider>
                                                                </ContractProvider>
                                                              </DataDogProvider>
                                                            </TaskProvider>
                                                          </PreferenceProvider>
                                                        </MutationProvider>
                                                      </ObjectHeaterProvider>
                                                    </SiteProvider>
                                                  </TerminalProvider>
                                                </HarvestPlanProvider>
                                              </HomeMarkerProvider>
                                            </FieldMarkerProvider>
                                          </MineProvider>
                                        </FileControllerProvider>
                                      </TransactionProvider>
                                    </FieldProvider>
                                  </GrainBagProvider>
                                </BinYardProvider>
                              </InteractionProvider>
                            </ComponentProvider>
                          </FirmwareProvider>
                        </TagProvider>
                      </NotificationProvider>
                    </BackpackProvider>
                  </GroupProvider>
                </DeviceProvider>
              </NoteProvider>
            </GateProvider>
          </BinProvider>
        </PermissionProvider>
      </TeamProvider>                                
    </UserProvider>
  );
}

export {
  useBackpackAPI,
  useComponentAPI,
  useDeviceAPI,
  useFirmwareAPI,
  useUserAPI,
  useTagAPI,
  useTeamAPI,
  useImagekitAPI,
  useInteractionsAPI,
  usePermissionAPI,
  useBinAPI,
  useBinYardAPI,
  useGateAPI,
  useGroupAPI,
  useGrainBagAPI,
  useNoteAPI,
  useNotificationAPI,
  useFieldAPI,
  useTransactionAPI,
  useFileControllerAPI,
  useMineAPI,
  useFieldMarkerAPI,
  useHomeMarkerAPI,
  useHarvestPlanAPI,
  useTerminalAPI,
  useSiteAPI,
  useObjectHeaterAPI,
  useMutationAPI,
  usePreferenceAPI,
  useTaskAPI,
  useDataDogProxyAPI,
  useContractAPI,
  useUsageAPI,
  useKeyManagerAPI,
  useJohnDeereProxyAPI,
  useCNHiProxyAPI,
  useLibraCartProxyAPI,
  useDevicePresetAPI
};
