import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { createStructuredSelector } from 'reselect';
import { GraphScreen } from '../components/graph-screen';
import reducer from '../reducer';
import { makeGraphSelector } 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 '../../track/epics';
import { updatePageMeta, PAGES } from '../../analytics';
import { withNavigationSync } from '../../common/hoc/with-navigation-sync';
import {
  setLandscapeOrientation,
  setPortraitOrientation,
} from '../../common/utils/device-utils';
import { TRootTiktokKpiState, TState } from '../types';
import { TRoute } from '../../tiktok/types';

type TStructured = {
  graph: TState['graph'];
};

export type TPropsExternal = {
  route: TRoute;
};

export type TPropsConnected = TStructured & TPropsExternal & {};

const mapStateToProps = createStructuredSelector<
  TRootTiktokKpiState,
  TStructured
>({
  graph: makeGraphSelector(),
  //†prop
});
const mapDispatchToProps = {
  // action1: ACTION.action1,
  //†action
};

// Uncomment to write componentDidMount hook
const Connected: React.FC<TPropsConnected> = props => {
  const { graph, route } = props;
  const { params: routeParams } = route;

  useEffect(() => {
    updatePageMeta({
      page: PAGES.Tiktokgraph.page,
      source_track_id: _.path('trackId', routeParams),
      source_track_isrc: _.path('isrc', routeParams),
      source_track_name: _.path('name', routeParams),
    });
  }, []);

  useEffect(() => {
    setLandscapeOrientation();

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

  return <GraphScreen graph={graph} />;
};

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