import React from 'react';
import ReactDOM from 'react-dom';
import './index.less';
import { Checkbox } from 'antd';
import Assets from '@src/components/Assets';
import Page from '@src/containers/Page';
import Button from '../../../components/Button';
import Navigation from '../../../components/Navigation';
import Answer from '../../../components/Answer';
import Calculator from '../../../components/Calculator';
import AnswerSelect from '../../../components/AnswerSelect';
import AnswerTable from '../../../components/AnswerTable';
import { Question } from '../../../stores/question';
import Editor from '../../../components/Editor';

export default class extends Page {
  initState() {
    return {
      showCalculator: false,
      start: !this.props.core.query.r,
      reportId: this.props.core.query.r,
      type: this.props.core.query.type,
      disorder: false,
      step: 0,
      info: {},
      reportInfo: {},
      questionInfo: {},
      answer: {},
    };
  }

  initData() {
    const { start } = this.state;
    Question.getPaper(this.params.id).then(result => {
      this.setState({ info: result });
    });
    if (!start) {
      this.continue();
    }
  }

  onChangeQuestion(index, value) {
    const { question = {}, answer = {} } = this.state;
    answer.questions[index] = { [question.type]: value };
    this.setState({ answer });
  }

  onChangeAwa(value) {
    const { answer = {} } = this.state;
    answer.awa = value;
    this.setState({ answer });
  }

  start() {
    const { type, info, disorder } = this.state;
    Question.start(type, info.id, disorder).then(result => {
      this.setState({ reportInfo: result });
      this.next();
    });
  }

  continue() {
    const { reportId } = this.state;
    Question.continue(reportId).then(result => {
      this.setState({ reportInfo: result });
      this.next();
    });
  }

  next() {
    const { reportInfo } = this.state;
    Question.next(reportInfo.id).then(result => {
      this.setState({
        questionInfo: result,
        question: result.question,
        answer: { questions: [], subject: [], predicate: [], object: [], options: [], awa: '' },
      });
    });
  }

  submit() {
    const { question, answer } = this.state;
    Question.submit(question.questionNoId, answer).then(() => {
      this.next();
    });
  }

  finish() {
    const { reportInfo } = this.state;
    Question.finish(reportInfo.id).then(() => {});
  }

  formatStrem(text) {
    if (!text) return '';
    const { question = { content: {} } } = this.state;
    const { table = {}, questions = [] } = question.content;
    text = text.replace(/#select#/g, "<span class='#select#' />");
    text = text.replace(/#table#/g, "<span class='#table#' />");
    setTimeout(() => {
      const selectList = document.getElementsByClassName('#select#');
      const tableList = document.getElementsByClassName('#table#');
      for (let i = 0; i < selectList.length; i += 1) {
        ReactDOM.render(
          <AnswerSelect list={questions[i].select} onChange={v => this.onChangeQuestion(i, v)} />,
          selectList[i],
        );
      }
      for (let i = 0; i < tableList.length; i += 1) {
        ReactDOM.render(
          <AnswerTable list={table.header || []} columns={table.header} data={table.data} />,
          tableList[i],
        );
      }
    }, 1);
    return text;
  }

  renderView() {
    const { start } = this.state;
    if (!start) return this.renderStart();
    return this.renderDetail();
  }

  renderCotent() {
    const { question = { content: {} }, step } = this.state;
    const { steps = [] } = question.content;
    return (
      <div className="block block-content">
        {steps.length > 0 && <Navigation list={question.content.steps} active={step} onChange={() => {}} />}
        <div className="text">{this.formatStrem(steps.length > 0 ? steps[step].stem : question.stem)}</div>
      </div>
    );
  }

  renderAnswer() {
    const { question = { content: {} } } = this.state;
    const { questions = [] } = question.content;
    if (question.type === 'inline') return '';
    return (
      <div className="block block-answer">
        <Editor onChange={v => this.onChangeAwa(v)} />
        {question.type === 'awa' && <Editor onChange={v => this.onChangeAwa(v)} />}
        {questions.map((item, index) => {
          return (
            <div>
              <div className="text m-b-2">{item.description}</div>
              <Answer
                list={item.select}
                type={question.type}
                direction={question.direction}
                onChange={v => this.onChangeQuestion(index, v)}
              />
            </div>
          );
        })}
      </div>
    );
  }

  renderDetail() {
    const { showCalculator, info, question = { content: {} } } = this.state;
    const { typeset = 'one' } = question.content;
    return (
      <div className="layout">
        <div className="fixed">
          Analytical Writing Assessment
          <Assets
            className="calculator-icon"
            name="calculator_icon"
            onClick={() => this.setState({ showCalculator: !showCalculator })}
          />
          <Assets className="collect-icon" name="collect_icon" />
        </div>
        <Calculator show={showCalculator} />
        <div className="layout-header">
          <div className="title">{info.title}</div>
          <div className="right">
            <div className="block">
              <Assets name="timeleft_icon" />
              Time left 00:02
            </div>
            <div className="block">
              <Assets name="subjectnumber_icon" />
              {question.no} of {info.questionNumer}
            </div>
          </div>
        </div>
        <div className={`layout-body ${typeset}`}>
          {this.renderCotent()}
          {this.renderAnswer()}
        </div>
        <div className="layout-footer">
          <div className="help">
            <Assets name="help_icon" />
            Help
          </div>
          <div className="full">
            <Assets name="fullscreen_icon" />
          </div>
          <div className="next" onClick={() => this.next()}>
            Next
            <Assets name="next_icon" />
          </div>
        </div>
      </div>
    );
  }

  renderStart() {
    const { info, disorder } = this.state;
    return (
      <div className="start">
        <div className="bg" />
        <div className="fixed-content">
          <div className="title">{info.title}</div>
          <div className="desc">
            <div className="block">
              <div className="desc-title">
                <Assets name="subject_icon" />
                题目总数
              </div>
              <div className="desc-info">{info.questionNumer}</div>
            </div>
            <div className="block">
              <div className="desc-title">
                <Assets name="time_icon" />
                建议用时
              </div>
              <div className="desc-info">{info.time}</div>
            </div>
          </div>
          <div className="tip">
            <Checkbox className="m-r-1" checked={disorder} onChange={() => this.setState({ disorder: !disorder })} />
            题目选项乱序显示
          </div>
          <div className="submit">
            <Button size="lager" radius onClick={() => this.start()}>
              开始练习
            </Button>
          </div>
        </div>
      </div>
    );
  }
}
