import React, { createContext, useContext, useEffect, useState } from 'react';

import { NetworksDataState } from '../types';
import { retrieveNetworksData, storeNetworkData } from '../utils/accounts';
import { DEFAULT_NETWORKS, EIP155 } from '../utils/constants';

const NetworksContext = createContext<{
  currentIndex: number;
  setCurrentIndex: (index: number) => void;
  networksData: NetworksDataState[];
  setNetworksData: React.Dispatch<React.SetStateAction<NetworksDataState[]>>;
  networkType: string;
  setNetworkType: (networkType: string) => void;
  selectedNetwork?: NetworksDataState;
  setSelectedNetwork: React.Dispatch<
    React.SetStateAction<NetworksDataState | undefined>
  >;
}>({
  currentIndex: 0,
  setCurrentIndex: () => {},
  networksData: [],
  setNetworksData: () => {},
  networkType: '',
  setNetworkType: () => {},
  selectedNetwork: {} as NetworksDataState,
  setSelectedNetwork: () => {},
});

const useNetworks = () => {
  const networksContext = useContext(NetworksContext);
  return networksContext;
};

const NetworksProvider = ({ children }: { children: any }) => {
  const [networksData, setNetworksData] = useState<NetworksDataState[]>([]);
  const [currentIndex, setCurrentIndex] = useState<number>(0);
  const [networkType, setNetworkType] = useState<string>(EIP155);
  const [selectedNetwork, setSelectedNetwork] = useState<NetworksDataState>();

  useEffect(() => {
    const fetchData = async () => {
      const retrievedNetworks = await retrieveNetworksData();
      if (retrievedNetworks.length === 0) {
        for (const defaultNetwork of DEFAULT_NETWORKS) {
          await storeNetworkData(defaultNetwork);
        }
      }
      const retrievedNewNetworks = await retrieveNetworksData();
      setNetworksData(retrievedNewNetworks);
      setSelectedNetwork(retrievedNewNetworks[0]);
    };

    fetchData();
  }, []);

  return (
    <NetworksContext.Provider
      value={{
        currentIndex,
        setCurrentIndex,
        networksData,
        setNetworksData,
        networkType,
        setNetworkType,
        selectedNetwork,
        setSelectedNetwork,
      }}>
      {children}
    </NetworksContext.Provider>
  );
};

export { useNetworks, NetworksProvider };
