import React from 'react';

import { autobind } from 'core-decorators';

import PageConfig from './config';
import layoutConfig from './layoutConfig';
import customConfig from './customConfig';
import customLayoutConfig from './customLayoutConfig';
import { longRunExec } from '../system/longRunOpt';
import { PFetch } from '../system/fetch';
import createLayout from '../layoutFrameWork';
import { ExtendButton } from '../../components';
import { BUTTON_RELATEDDATA } from '../constant/dictCodes';
import { isApply, isReview } from '../utils/ProcessHelper';

const createPage = (domainType, domainLink, query) => {
  const page = customConfig(domainLink, query) ? customConfig(domainLink, query) : PageConfig[domainType];
  return page || PageConfig.default;
};

const InitDecorator = () => {
  class WrapperComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        data: null
      };
    }

    @autobind
    _createRefs() {
      const ref = 'pageContainer_' + Math.random();
      this.refStr = ref;
      return ref;
    }

    @autobind
    renderLayout() {
      const WrapperTitle = createLayout(this.props.layoutType);
      return (
        <WrapperTitle
          {...this.props}
          globalBtns={this.getGlobalBtns()}
        />
      );
    }

    @autobind
    getGlobalBtns() {
      if (this.props.domainType === 'Info') {
        const { prefixCls, query, inject, dataSource, domainLink } = this.props;
        const buttonClass = `${prefixCls}-button`;
        const buttons = (dataSource && dataSource.buttons) || [];
        const menuCode = query.processMenuId;
        const readonly = query.readonly === 'true' || query.readonly === true || dataSource.readOnly || query.displayType === 'VIEW';
        const globalBtns = readonly || ((isApply(domainLink) || isReview(domainLink)) &&
        (menuCode && menuCode !== 'BPMApply.QueryToDo' && menuCode !== 'BPMReview.QueryToDo' && menuCode !== 'BPMApply.QueryReturned')) ? [] :
          buttons.map(item => (
            <ExtendButton
              type="link"
              inline={false}
              buttonProps={{ type: 'ghost' }}
              {...item}
              disabled={false}
              key={item.buttonDescription}
              query={query}
              className={buttonClass}
              inject={inject}
              relatedRows={BUTTON_RELATEDDATA.NONE}
              onRefresh={() => console.log('!!!!!!!-----onRefresh')}
            />
          ));
        return globalBtns;
      }
      return null;
    }

    componentDidMount() {
      // remove loading if exist
    }

    render() {
      const Wrapper = createPage(this.props.domainType, this.props.domainLink, this.props.query);
      return (
        <div style={{ height: '100%' }}>
          {this.props.query && this.props.query.s === '1' && this.renderLayout()}
          <Wrapper
            {...this.props}
            ref={this._createRefs}
            exec={longRunExec}
            fetch={PFetch}
            resetPosition={this.props.resetPosition}
            initWrapperHeight={this.props.initWrapperHeight}
            bodyAutoScroll={this.props.bodyAutoScroll}
            globalBtns={this.getGlobalBtns()}
          />
        </div>
      );
    }
  }

  return WrapperComponent;
};

export default InitDecorator;

