import { createSlice } from "@reduxjs/toolkit";
import type { Token } from "../Providers/ConfigProvider";
import { updateLocalStorage } from "./settingsSlice";

export enum ScreenTab {
	HOME = "home",
	REVIEW = "review",
	INFLIGHT = "inflight",
}

const bungeeLocalStore =
	typeof window !== "undefined"
		? JSON.parse(localStorage.getItem("bungee") || "{}")
		: {};

const initialState: GlobalParams = {
	supportedChains: [],
	shortTokenList: [],
	largeTokenList: [],
	showRecipientModal: false,
	showCompareModal: false,
	showHistoryModal: false,
	screenTab: ScreenTab.HOME,
	importedTokens: bungeeLocalStore?.tokens?.imported,
};

const globalStates = createSlice({
	name: "global",
	initialState,
	reducers: {
		setSupportedChains: (state, action) => {
			state.supportedChains = action.payload;
		},
		setShortTokenList: (state, action) => {
			state.shortTokenList = action.payload;
		},
		setLargeTokenList: (state, action) => {
			state.largeTokenList = action.payload;
		},
		toggleRecipientModal: (state, action) => {
			state.showRecipientModal = action.payload;
		},
		toggleCompareModal: (state, action) => {
			state.showCompareModal = action.payload;
		},
		setScreenTab: (state, action) => {
			state.screenTab = action.payload;
		},
		toggleHistoryModal: (state, action) => {
			state.showHistoryModal = action.payload;
		},
		importToken: (state, action: { payload: { token: Token } }) => {
			let importedTokens = state.importedTokens;
			if (!importedTokens) importedTokens = {};

			const id = action.payload.token.chainId;
			const tags = action.payload.token?.tags;
			const updatedTags = tags?.length ? [...tags, "imported"] : ["imported"];
			const token = { ...action.payload.token, tags: updatedTags };

			if (!importedTokens?.[id]) importedTokens[id] = [token];
			else {
				const _tokens = importedTokens[id];
				const tokenExists =
					_tokens.filter(
						(existingToken) =>
							existingToken.address.toLowerCase() === token.address.toLowerCase(),
					)?.length > 0;
				if (!tokenExists) importedTokens[id].push(token);
			}

			state.importedTokens = importedTokens;
			updateLocalStorage("tokens", "imported", importedTokens);
		},
	},
});

export const {
	setSupportedChains,
	setShortTokenList,
	setLargeTokenList,
	toggleRecipientModal,
	toggleCompareModal,
	setScreenTab,
	toggleHistoryModal,
	importToken,
} = globalStates.actions;
export default globalStates.reducer;

export interface GlobalParams {
	supportedChains: Chain[];
	shortTokenList: { [chainId: number]: Token[] };
	largeTokenList: { [chainId: number]: Token[] };
	showRecipientModal: boolean;
	showCompareModal: boolean;
	showHistoryModal: boolean;
	screenTab: ScreenTab;
	importedTokens?: { [chainId: number]: Token[] };
}

export interface Chain {
	chainId: number;
	name: string;
	isL1: boolean;
	sendingEnabled: boolean;
	icon: string;
	receivingEnabled: boolean;
	rpcs: string[];
	explorers: string[];
	currency: Currency;
}

interface Currency extends Token {
	minNativeCurrencyForGas: string;
}
