import React from 'react';
import { bool, string, func, node, arrayOf, shape } from 'prop-types';
import styled from 'styled-components';
import { REM_UNIT_LG, GRAY_X11, CHARLESTON_GREEN, SONIC_SILVER } from '@vincent_za/styles';
import { PROP_ASYNC_STATUS, SHAPE_ASYNC_STATUS_INITIAL } from '@vincent_za/utils';
import { fullscreenSvg } from '@vincent_za/assets';
import scrollHOC from '../Scroll/component';
import { SpinnerLoader, LineLoader } from '../Loader';
import { ErrorInline } from '../Error';
import Svg from '../Svg/component';

export const CN_INNER_WRAP = 'pane-inner-wrap';
export const CN_RELATIVE_WRAP = 'pane-relative-wrap';

const OuterWrap = styled.div`
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  padding: ${REM_UNIT_LG};

  ${props => props.isFullscreen && `
    position: absolute;
    z-index: 1;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0 !important;
    width: 100% !important;
    height: 100% !important;
    background-color: ${CHARLESTON_GREEN};
  `};
`;

const RelativeWrap = styled.div`
  display: flex;
  flex-direction: column;
  position: relative;
  height: 100%;
  background-color: ${CHARLESTON_GREEN};
  box-shadow: 0 0.6rem 11px -0.4rem #1e1e1e69, 0 0 16px -2px #1e1e1e69;
`;

const Header = styled.header`
  padding: 16px;
  height: 44px;
  line-height: 1;
  text-transform: uppercase;
  font-family: Lato-bold, sans-serif;
  color: #607D8B;
`;

const InnerWrap = styled.div`
  display: flex;
  flex-direction: column;
  padding: ${REM_UNIT_LG};
  height: 100%;
  line-height: 1.5;
  word-wrap: break-word;
  color: ${SONIC_SILVER};
  overflow: auto;
  ${props => props.isFullscreen && 'box-shadow: none;'};
`;

const InnerWrapScroll = scrollHOC(InnerWrap);

const ErrorInlineStyled = styled(ErrorInline)`
  padding: 0 ${REM_UNIT_LG};
`;

const Icon = styled(Svg)`
  position: absolute;
  top: ${REM_UNIT_LG};
  right: ${REM_UNIT_LG};
  width: 1.2rem;
  height: 1.2rem;
  fill: ${GRAY_X11};
  opacity: ${props => props.isFullscreen ? 1 : 0.3};

  &:hover {
    opacity: 1;
  };
`;

class Pane extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isFullscreen: false,
    };
  }

  shouldComponentUpdate(nextProps, nextState) {
    if (nextState.isFullscreen !== this.state.isFullscreen) return true;
    if (nextProps.children !== this.props.children) return true;
    if (nextProps.asyncStatus !== this.props.asyncStatus) return true;
    if (nextProps.asyncStatusList !== this.props.asyncStatusList) return true;
    return false;
  }

  onFullscreenToggle = () => {
    const { isFullscreen } = this.state;
    const { width, height } = this.innerRef.getBoundingClientRect();
    this.props.onFullscreenToggleCb({ isFullscreen: !isFullscreen, contentDimensions: { width, height } });
    this.setState({
      isFullscreen: !isFullscreen,
    });
  }

  addInnerRef = (el) => { this.innerRef = el; };

  render() {
    const { isFullscreen } = this.state;
    const {
      isTouchDevice,
      title,
      children,
      className,
      asyncStatus,
      asyncStatusList,
    } = this.props;

    return (
      <OuterWrap isFullscreen={isFullscreen} className={className}>
        <RelativeWrap className={CN_RELATIVE_WRAP}>
          {title && <Header>{title}</Header>}
          <Icon isFullscreen={isFullscreen} svg={fullscreenSvg} onClick={this.onFullscreenToggle} />
          <InnerWrapScroll
            isTouchDevice={isTouchDevice}
            isFullscreen={isFullscreen}
            className={CN_INNER_WRAP}
            passedRef={this.addInnerRef}
          >
            <LineLoader isDisplayOnLoaded asyncStatus={asyncStatus} asyncStatusList={asyncStatusList} />
            <SpinnerLoader asyncStatus={asyncStatus} asyncStatusList={asyncStatusList} />
            <ErrorInlineStyled asyncStatus={asyncStatus} asyncStatusList={asyncStatusList} />
            {children}
          </InnerWrapScroll>
        </RelativeWrap>
      </OuterWrap>
    );
  }
}

Pane.propTypes = {
  isTouchDevice: bool.isRequired,
  title: string,
  children: node.isRequired,
  className: string,
  onFullscreenToggleCb: func,
  asyncStatus: shape(PROP_ASYNC_STATUS),
  asyncStatusList: arrayOf(shape(PROP_ASYNC_STATUS)),
};

Pane.defaultProps = {
  title: undefined,
  className: undefined,
  onFullscreenToggleCb() {},
  asyncStatus: SHAPE_ASYNC_STATUS_INITIAL,
  asyncStatusList: [],
};

export default Pane;
