import { Ionicons } from "@expo/vector-icons";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import i18n from "i18next";
import React, { useEffect } from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import {
  checkUserSession,
  emailSignInStart,
  signOutStart,
} from "../../redux/user/user.actions";
import {
  selectBodyshop,
  selectCurrentUser,
} from "../../redux/user/user.selectors";
//import ScreenCamera from "../screen-camera/screen-camera";
import ScreenJobDetail from "../screen-job-detail/screen-job-detail.component";
import ScreenJobList from "../screen-job-list/screen-job-list.component";
import ScreenMediaBrowser from "../screen-media-browser/screen-media-browser.component";
//import ScreenMediaCache from "../screen-media-cache/screen-media-cache.component";
//import ScreenMessagingConversation from "../screen-messaging-conversation/screen-messaging-conversation.component";
//import ScreenMessagingList from "../screen-messaging-list/screen-messaging-list.component";
import ScreenSettingsComponent from "../screen-settings/screen-settings.component";
import ScreenSignIn from "../screen-sign-in/screen-sign-in.component";
import ScreenSplash from "../screen-splash/screen-splash.component";

const JobStack = createStackNavigator();
//const CameraStack = createStackNavigator();
//const MessagingStack = createStackNavigator();
//const MediaCacheStack = createStackNavigator();
const MoreStack = createStackNavigator();
const BottomTabs = createBottomTabNavigator();
const MediaBrowserStack = createStackNavigator();

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
  currentUser: selectCurrentUser,
});

const mapDispatchToProps = (dispatch) => ({
  checkUserSession: () => dispatch(checkUserSession()),
  emailSignInStart: (email, password) =>
    dispatch(emailSignInStart({ email, password })),
  signOutStart: () => dispatch(signOutStart()),
});

const JobStackNavigator = () => (
  <JobStack.Navigator initialRouteName="JobList">
    <JobStack.Screen
      name="JobList"
      options={() => ({
        title: i18n.t("joblist.labels.activejobs"),
      })}
      component={ScreenJobList}
    />
    <JobStack.Screen
      name="JobDetail"
      component={ScreenJobDetail}
      options={({ route }) => ({
        title:
          (route.params && route.params.title) ||
          i18n.t("joblist.labels.detail"),
      })}
    />
    {/* <JobStack.Screen
      name="MediaBrowser"
      component={ScreenMediaBrowser}
      // options={({ route }) => ({
      //   title:
      //     (route.params && route.params.title) ||
      //     i18n.t("joblist.labels.detail"),
      // })}
    /> */}
  </JobStack.Navigator>
);

// const CameraStackNavigator = () => (
//   <CameraStack.Navigator initialRouteName="TabCameraScreen">
//     <CameraStack.Screen
//       name="TabCameraScreen"
//       options={{ headerShown: false }}
//       component={ScreenCamera}
//     />
//   </CameraStack.Navigator>
// );

// const MediaCacheStackNavigator = () => (
//   <MediaCacheStack.Navigator initialRouteName="TabMediaCache">
//     <MediaCacheStack.Screen
//       name="MediaCache"
//       options={{ title: i18n.t("mediacache.titles.mediacachetab") }}
//       component={ScreenMediaCache}
//     />
//   </MediaCacheStack.Navigator>
// );

const MediaBrowserStackNavigator = () => (
  <MediaBrowserStack.Navigator initialRouteName="MediaBrowser">
    <MediaBrowserStack.Screen
      name="MediaBrowser"
      options={{ title: i18n.t("mediabrowser.titles.mediabrowsertab") }}
      component={ScreenMediaBrowser}
    />
  </MediaBrowserStack.Navigator>
);

// const MessagingStackNavigator = () => (
//   <MessagingStack.Navigator>
//     <MessagingStack.Screen
//       name="MessagingList"
//       component={ScreenMessagingList}
//     />
//     <MessagingStack.Screen
//       name="MessagingConversation"
//       component={ScreenMessagingConversation}
//     />
//   </MessagingStack.Navigator>
// );

const MoreStackNavigator = () => (
  <MoreStack.Navigator>
    <MoreStack.Screen
      name="Settings"
      options={{
        title: i18n.t("settings.titles.settings"),
      }}
      component={ScreenSettingsComponent}
    />
  </MoreStack.Navigator>
);

const BottomTabsNavigator = () => (
  <BottomTabs.Navigator
    screenOptions={({ route }) => ({
      // eslint-disable-next-line react/display-name
      tabBarIcon: ({ color, size }) => {
        let iconName;
        if (route.name === "JobTab") {
          iconName = "ios-list";
        } else if (route.name === "MessagingTab") {
          iconName = "ios-chatboxes";
        } else if (route.name === "CameraTab") {
          iconName = "ios-camera";
        } else if (
          route.name === "MediaCacheTab" ||
          route.name === "MediaBrowserTab"
        ) {
          iconName = "ios-photos";
        } else {
          //iconName = "customerservice";
        }

        return <Ionicons name={iconName} size={size} color={color} />;
      },
    })}
    tabBarOptions={{
      activeTintColor: "dodgerblue",
      inactiveTintColor: "slategrey",
    }}
  >
    <BottomTabs.Screen
      name="JobTab"
      options={{ title: i18n.t("joblist.titles.jobtab") }}
      component={JobStackNavigator}
    />
    <BottomTabs.Screen
      name="MediaBrowserTab"
      options={{ title: i18n.t("mediabrowser.titles.mediabrowsertab") }}
      component={MediaBrowserStackNavigator}
    />
    {/* <BottomTabs.Screen
      name="MediaCacheTab"
      options={{ title: i18n.t("mediacache.titles.mediacachetab") }}
      component={MediaCacheStackNavigator}
    /> */}
    {/* <BottomTabs.Screen
      name="CameraTab"
      options={{ title: i18n.t("camera.titles.cameratab") }}
      component={CameraStackNavigator}
    />   <BottomTabs.Screen
      name="MessagingTab"
      options={{ title: i18n.t("messaging.titles.messagingtab") }}
      component={MessagingStackNavigator}
    /> */}
    <BottomTabs.Screen
      name="MoreTab"
      options={{ title: i18n.t("more.titles.moretab") }}
      component={MoreStackNavigator}
    />
  </BottomTabs.Navigator>
);

export function ScreenMainComponent({
  checkUserSession,
  currentUser,
  bodyshop,
}) {
  useEffect(() => {
    checkUserSession();
  }, [checkUserSession]);

  return (
    <NavigationContainer>
      {currentUser.authorized === null ? (
        <ScreenSplash />
      ) : currentUser.authorized ? (
        bodyshop ? (
          <BottomTabsNavigator />
        ) : (
          <ScreenSplash />
        )
      ) : (
        <ScreenSignIn />
      )}
    </NavigationContainer>
  );
}
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ScreenMainComponent);
