import {NavigationContainer} from '@react-navigation/native';
import React, {useState} from 'react';
import {Provider} from 'react-redux';
import {Channel as ChannelType, StreamChat} from 'stream-chat';
import {OverlayProvider} from 'stream-chat-react-native';
import {navigationRef} from './RootNavigation';
import Routes from './routes';
import store from './store/configureStore';
import {
  ChatContextType,
  LocalAttachmentType,
  LocalChannelType,
  LocalCommandType,
  LocalEventType,
  LocalMessageType,
  LocalResponseType,
  LocalUserType,
} from './types';

export const ChatContext = React.createContext({} as ChatContextType);

const App = () => {
  const [channel, setChannel] = useState<
    ChannelType<
      LocalAttachmentType,
      LocalChannelType,
      LocalCommandType,
      LocalEventType,
      LocalMessageType,
      LocalResponseType,
      LocalUserType
    >
  >();
  const chatClient = StreamChat.getInstance<
    LocalAttachmentType,
    LocalChannelType,
    LocalCommandType,
    LocalEventType,
    LocalMessageType,
    LocalResponseType,
    LocalUserType
  >('27yt628q52ht');
  return (
    /**
     * This is the provider from the redux store, it acts as the root provider for our application
     */
    <Provider store={store}>
      <NavigationContainer ref={navigationRef}>
        <ChatContext.Provider value={{channel, setChannel, chatClient}}>
          <OverlayProvider>
            <Routes />
          </OverlayProvider>
        </ChatContext.Provider>
      </NavigationContainer>
    </Provider>
  );
};

export default App;
