import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { createStructuredSelector } from 'reselect';
import { NavigationProp } from '@react-navigation/native';
import { GraphScreen } from '../components/graph-screen';
import reducer from '../reducer';
import { makeLineGraphSelector, makeVisibilitySelector } from '../selectors';
import { fp as _ } from '../../common/utils/fp';
import { withErrorBoundary } from '../../common/hoc/with-error-boundary';
import { withReducer } from '../../app/core/with-reducer';
import { withEpics } from '../../app/core/with-epics';
import * as epics from '../epics';
import { updatePageMeta, PAGES } from '../../analytics';
import { withNavigationSync } from '../../common/hoc/with-navigation-sync';
import {
  setLandscapeOrientation,
  setPortraitOrientation,
} from '../../common/utils/device-utils';
import { TMarketsStreamsRootState, TNavProps, TVisibility } from '../types';
import { TGraph } from '../../graph/types';

type TStructured = {
  visibility: TVisibility;
  graph: TGraph;
};

export type TPropsExternal = {
  navigation: NavigationProp<any>;
  route: {
    params: TNavProps;
  };
};

export type TPropsConnected = TStructured & TPropsExternal;

const mapStateToProps = createStructuredSelector<
  TMarketsStreamsRootState,
  TStructured
>({
  graph: makeLineGraphSelector(),
  visibility: makeVisibilitySelector(),
  //†prop
});
const mapDispatchToProps = {
  // action1: ACTION.action1,
  //†action
};

// Uncomment to write componentDidMount hook
const Connected: React.FC<TPropsConnected> = props => {
  useEffect(() => {
    updatePageMeta({
      page: PAGES.Graphmulti.page,
      detailed_graph_type: 'Combined Streams',
    });
  }, []);

  useEffect(() => {
    setLandscapeOrientation();

    return () => {
      setPortraitOrientation();
    };
  }, []);

  return <GraphScreen {...props} />;
};

export default compose(
  _.hoistStatics(GraphScreen),
  withReducer('marketsStreams', reducer),
  withEpics('marketsStreams', epics),
  withErrorBoundary('marketsStreams:graph-screen'),
  withNavigationSync(),
  connect(
    mapStateToProps,
    mapDispatchToProps
  )
)(Connected) as React.FC;
