import React, {
  ReactNode,
  useContext,
  useEffect,
  useMemo,
  useState,
} from "react";
import { useSettingsContext } from "./SettingsContext";

export type Environment = "mainnet" | "testnet" | "devnet";
export type Network = {
  env: Environment;
  endpoint: string;
  name: string;
  logo: string;
};

type NetworkContextValue = {
  currentNetwork: Network;
  setCurrentNetwork: React.Dispatch<React.SetStateAction<Network>>;
};

// https://book.wormhole.com/reference/rpcnodes.html
// https://wormhole.com/security/
export const networkOptions: Network[] = [
  {
    env: "mainnet",
    endpoint: "https://wormhole-v2-mainnet-api.certus.one",
    name: "Jump Crypto",
    logo: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjMiIGhlaWdodD0iMjEiIHZpZXdCb3g9IjAgMCA2MyAyMSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGcgY2xpcC1wYXRoPSJ1cmwoI2NsaXAwXzQ4NzZfNTYzKSI+CjxwYXRoIGQ9Ik01LjUwMTkgNS40MjE2N0g2LjIxMDQ2VjE2LjM3MjlDNi4yMTA0NiAxOC43OTc4IDQuMzUxNjkgMjAuNDMzOSAwLjc3NTM5MSAyMC40MzM5VjE4LjEzMTFDMi4zMTEgMTguMTMxMSAzLjA3ODIgMTcuNDg0OCAzLjA3ODIgMTYuMTcwN1Y1LjQyMTY3SDUuNTAxOVpNMy40MjE3MSAzLjY0MzE0QzMuMjU4NiAzLjQ4NDAzIDMuMTI4OTcgMy4yOTM4OSAzLjA0MDQ2IDMuMDgzOTJDMi45NTE5NiAyLjg3Mzk2IDIuOTA2MzYgMi42NDg0IDIuOTA2MzYgMi40MjA1NEMyLjkwNjM2IDIuMTkyNjkgMi45NTE5NiAxLjk2NzEzIDMuMDQwNDYgMS43NTcxNkMzLjEyODk3IDEuNTQ3MTkgMy4yNTg2IDEuMzU3MDYgMy40MjE3MSAxLjE5Nzk1QzMuNTgwODIgMS4wMzQ4NCAzLjc3MDk2IDAuOTA1MjIgMy45ODA5M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S45ODkyIDQ1Ljc1ODYgMTUuOTg5MkM0NC4zODQ1IDE1Ljk4OTIgNDMuMjczIDE1LjU0NDggNDIuNDI0IDE0LjY1NTlWMjAuMjMzM0gzOS4yOTNWNS40MjIxN0g0Mi40MjRWNi41MzY0NUM0My4yNzMgNS42NDgzOCA0NC4zODQ1IDUuMjAzOTQgNDUuNzU4NiA1LjIwMzE0QzQ3LjMxNDUgNS4xOTk1NSA0OC41ODggNS42ODQyOCA0OS41OTgyIDYuNjM0NTlaTTQ3LjgzOTkgMTAuNTk1QzQ3LjgzOTkgOC43MzYyNyA0Ni42ODg1IDcuNTAzNTEgNDUuMTMyNiA3LjUwMzUxQzQzLjU5NyA3LjUwMzUxIDQyLjQyNCA4LjY3NTIzIDQyLjQyNCAxMC41OTVDNDIuNDI0IDEyLjUxNDggNDMuNTk3IDEzLjY4NjUgNDUuMTMyNiAxMy42ODY1QzQ2LjY4ODUgMTMuNjg2NSA0Ny44Mzk5IDEyLjQ1MzcgNDcuODM5OSAxMC41OTVaIiBmaWxsPSJ3aGl0ZSIvPgo8L2c+CjxwYXRoIGQ9Ik02Mi4yMjQ2IDEyLjkyMDlMNjIuMjI0NiAxNS4zNjg2TDUyLjQzMzUgMTUuMzY4Nkw1Mi40MzM1IDEyLjkyMDlMNjIuMjI0NiAxMi45MjA5WiIgZmlsbD0id2hpdGUiLz4KPGRlZnM+CjxjbGlwUGF0aCBpZD0iY2xpcDBfNDg3Nl81NjMiPgo8cmVjdCB3aWR0aD0iNTAuMzM4NyIgaGVpZ2h0PSIxOS43NTA2IiBmaWxsPSJ3aGl0ZSIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoMC43NzUzOTEgMC42ODM1OTQpIi8+CjwvY2xpcFBhdGg+CjwvZGVmcz4KPC9zdmc+Cg==",
  },
  {
    env: "mainnet",
    endpoint: "https://wormhole.inotel.ro",
    name: "inotel",
    logo: "data:image/svg+xml;base64,[encoded data, 7412 characters]",
  },
  {
    env: "mainnet",
    endpoint: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DA4SDM4Ljc4NFYxMkgzNi40NDhaIiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4K",
  },
  {
    env: "mainnet",
    endpoint: "https://wormhole-v2-mainnet-api.chainlayer.network",
    name: "ChainLayer",
    logo: "https://wormhole.com/static/chainlayer-6e20d0207f0e732a015540e4aef0d6b5.svg",
  },
  {
    env: "mainnet",
    endpoint: "https://wormhole-v2-mainnet-api.staking.fund",
    name: "Staking Fund",
    logo: "https://wormhole.com/static/stakingfund-102552dce2737668d02b536d075ba3ab.svg",
  },
  {
    env: "mainnet",
    endpoint: "https://wormhole-v2-mainnet.01node.com",
    name: "01NODE",
    logo: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzgiIGhlaWdodD0iMjgiIHZpZXdCb3g9IjAgMCA3OCAyOCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTQwLjE2NzggNy4yOTM5NUg0My4yMDA4VjE5LjIzMzZINDAuODQ4NEwzNi42MzE4IDEzLjIyNjhWMTkuMjMzNkgzMy41ODRWNy4yOTM5NUgzNS45NTEyTDQwLjE2NzggMTMuMzAwOFY3LjI5Mzk1WiIgZmlsbD0id2hpdGUiLz4KPHBhdGggZD0iTTU1LjIxNjQgMTcuNjk1NUM1NC4wMDMyIDE4Ljg5MzkgNTIuNTIzNyAxOS41MDA1IDUwLjc3NzkgMTkuNTAwNUM0OS4wMzIxIDE5LjUwMDUgNDcuNTM3NyAxOC44OTM5IDQ2LjMzOTMgMTcuNjk1NUM0NS4xMjYxIDE2LjQ5NzEgNDQuNTE5NSAxNS4wMTc2IDQ0LjUxOTUgMTMuMjcxN0M0NC41MTk1IDExLjUyNTkgNDUuMTI2MSAxMC4wNDY0IDQ2LjMzOTMgOC44NDc5OEM0Ny41NTI1IDcuNjQ5NTcgNDkuMDMyMSA3LjA0Mjk3IDUwLjc3NzkgNy4wNDI5N0M1Mi41MjM3IDcuMD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4NDMgMTQuMTg4NiAyMC4xMzYyIDEwLjY4MjEgMjAuMTM2MlpNMjIuNjgxIDE5Ljg4NDdIMTkuODk5NVYxMC42NTI1TDE3LjczOTQgMTEuMjQ0M0wxNy4wNTg4IDguODYyMzFMMjAuMjM5OCA3LjcwODI4SDIyLjY4MVYxOS44ODQ3Wk0xMC42ODIxIDEwLjE3OTFDOC42OTk1NiAxMC4xNzkxIDcuMTMxMjcgMTEuNjI5IDcuMTMxMjcgMTMuODAzOUM3LjEzMTI3IDE1Ljk3ODggOC42OTk1NiAxNy40Mjg3IDEwLjY4MjEgMTcuNDI4N0MxMi42NjQ3IDE3LjQyODcgMTQuMjMzIDE1Ljk3ODggMTQuMjMzIDEzLjgwMzlDMTQuMjMzIDExLjYxNDIgMTIuNjc5NSAxMC4xNzkxIDEwLjY4MjEgMTAuMTc5MVoiIGZpbGw9IndoaXRlIi8+Cjwvc3ZnPgo=",
  },
  {
    env: "testnet",
    endpoint: "https://wormhole-v2-testnet-api.certus.one",
    name: "Testnet",
    logo: "",
  },
  {
    env: "devnet",
    endpoint: "http://localhost:7071",
    name: "Devnet",
    logo: "",
  },
];

const defaultNetwork: Network = networkOptions[0];

const NetworkContext = React.createContext<NetworkContextValue>({
  currentNetwork: defaultNetwork,
  setCurrentNetwork: () => {},
});

export const NetworkContextProvider = ({
  children,
}: {
  children: ReactNode;
}) => {
  const {
    settings: { defaultEndpoint },
    updateDefaultEndpoint,
  } = useSettingsContext();
  const [currentNetwork, setCurrentNetwork] = useState<Network>(
    networkOptions.find((option) => option.endpoint === defaultEndpoint) ||
      defaultNetwork
  );
  useEffect(() => {
    updateDefaultEndpoint(currentNetwork.endpoint);
  }, [currentNetwork, updateDefaultEndpoint]);
  const value = useMemo(
    () => ({ currentNetwork, setCurrentNetwork }),
    [currentNetwork, setCurrentNetwork]
  );
  return (
    <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
  );
};

export const useNetworkContext = () => {
  return useContext(NetworkContext);
};
