import React, { Component } from 'react';
import {CopyToClipboard} from 'react-copy-to-clipboard';
import AceEditor from 'react-ace';
import { Toast } from "antd-mobile"
import {HeaderBar} from '@/common';
import { browser, http, getParam } from '@/utils';
import './index.scss';
import 'ace-builds/src-noconflict/mode-python';
import 'ace-builds/src-noconflict/theme-dracula';

class PythonClass extends Component {

  constructor(props) {
    super(props);
    this.state = {
      isWechat: browser.isWeixin,
      isShare: true,
      type: '1', // 1:课后习题，2:课堂习题
      entryMode: 1, // 0：扫码页，1:落地页，2:直接进入
      isExecute: false,
      isCopy: false,
      command: 'http://m.julyedu.com/python?from=python',
      data: {}
    }
  }

  componentDidMount() {
    this.handleFetchInfo();
    this.initPageStatus();
  }

  initPageStatus = () => {
    if(getParam('from') === 'barcode') {
      this.setState({
        entryMode: 0
      });
    }
    if(getParam('from') === 'python') {
      this.setState({
        entryMode: 2
      });
    }
    if(getParam('type') === '1') {
      this.setState({
        isShare: true
      });
    }
    if(getParam('type') === '2') {
      this.setState({
        isShare: false
      });
    }
    this.setState({
      type: getParam('ques') || '1'
    });
  }

  handleFetchInfo = () => {
    const id = getParam('id') || 10;
    http.get(`${API.home}/web/python/share/help/${id}`).then(res => {
      console.log(res);
      const { code, data } = res.data;
      if(code === 200) {
        this.setState({
          data,
        });
      }
    })
  }

  handleToSend = () => {
    const { history } = this.props;
    if(browser.isWeixin) {
      this.setState({
        entryMode: 1
      });
      history.push(`/python?id=${getParam('id')}&type=${getParam('type')}&ques=${getParam('ques')}&from=python`);
    }
  }

  copyToSuccess = () => {
    Toast.info('已复制链接，快去粘贴发给好友吧～');
    this.setState({
      isCopy: true
    });
  }

  handleToExecute = () => {
    this.setState({
      isExecute: true
    });
  }

  render() {
    const { isWechat, isShare, isExecute, type, entryMode, command, isCopy, data } = this.state;
    return (
      <>
        <HeaderBar
          title='Python基础语法'
          arrow={true}
          home={true}
        />
        <PythonContent
          isWechat={isWechat}
          isShare={isShare}
          type={type}
          isExecute={isExecute}
          entryMode={entryMode}
          isCopy={isCopy}
          command={command}
          data={data}
          handleToExecute={this.handleToExecute}
          handleToSend={this.handleToSend}
          copyToSuccess={this.copyToSuccess}
        />
      </>
    );
  }
}

function SelfAceEditor(props) {
  return (
    <AceEditor 
      mode="python"
      theme="dracula"
      readOnly={true}
      showPrintMargin={false}
      value={props.code}
      style={{
        width: '100%',
        height: '100%'
      }}
    />
  )
}

function PythonContent(props) {
  const { 
    isWechat, 
    isShare, 
    isExecute, 
    type,
    entryMode, 
    isCopy, 
    command, 
    data: { head_img, nickname, code_lines, code, result, ques_name, course_name, video_name },
    handleToSend, 
    copyToSuccess, 
    handleToExecute 
  } = props;
  let labelName = '';
  if(type === '1') {
    labelName = `练习-${ques_name}`;
  }
  if(type === '2') {
    labelName = `课堂-${video_name}`;
  }

  return (
    <div className="python-container">

      {/* 微信 */}
      {
        // (entryMode === 0 && isWechat) && 
        // <div className="python-header">
        //   <p className="python-wechat__title">请点击右上角分享</p>
        //   <i className="iconfont iconyindao"></i>
        // </div>
      }

      {/* 非微信 */}
      {
        // (entryMode === 0 && !isWechat) && 
        // <div className="python-header">
        //   <p className="python-wechat__title">
        //     {isShare? '快分享给好友吧' : '快发给好友求助吧'}
        //   </p>
        // </div>
      }

      {
        (entryMode === 1 && isWechat) && 
        <div className="python-popup">
          <div className="python-header">
            <p className="python-wechat__title">请点击右上角分享</p>
            <i className="iconfont iconyindao"></i>
          </div>
        </div>
      }
      <div className="python-content">
        <div className="python-user">
          <div className="python-user__portrait">
            <img src={head_img} alt=""/>
          </div>
          <h2 className="python-user__id">{nickname}</h2>

          {/* 分享 */}
          {
            (entryMode === 0 && isShare) &&
            <p className="python-user__desc">
              完成了
              <span>【{labelName}】</span>
            </p>
          }
          {
            (entryMode !== 0 && isShare) &&
            <p className="python-user__desc">
              在
              <span>【{course_name}】</span>完成了<br />
              {labelName}
            </p>
          }

          {/* 求助 */}
          {
            (entryMode === 0 && !isShare) &&
            <p className="python-user__desc">
              在
              <span>【{labelName}】</span>
              遇到了困难
            </p>
          }
          {
            (entryMode !== 0 && !isShare) &&
            <p className="python-user__desc">
              在
              <span>【{course_name}】</span>的<br />
              <span>{labelName}</span>遇到了困难
            </p>
          }
        </div>
        <h4 className="python-code__title">
          {entryMode !== 0 && isShare? `这是Ta的第${code_lines}行代码` : '运行结果'}
        </h4>
        <div className="python-code__content">
          <SelfAceEditor code={entryMode !== 0 && isShare? code : result} />
        </div>
        <h4 className="python-code__title">{entryMode === 0? '代码' : '运行结果'}</h4>
        <div className="python-code__content">
          {
            entryMode === 0 && 
            <SelfAceEditor code={code} />
          }
          {
            entryMode !== 0 && 
            <SelfAceEditor code={isExecute? result : ''} />
          }
          {
            (entryMode !== 0 && isShare && !isExecute) &&
            <button className="python-button python-button__execute" onClick={handleToExecute}>运行看看</button>
          }
        </div>
      </div>

      {
        (entryMode === 0 && isWechat) &&
        <button className="python-button python-button__study" onClick={handleToSend}>发给好友</button>
      }

      {
        (entryMode === 0 && !isWechat && !isCopy) &&
        <CopyToClipboard
          text={command}
          onCopy={copyToSuccess}
        >
          <button className="python-button python-button__study">发给好友</button>
        </CopyToClipboard>
      }

      {
        (entryMode === 0 && !isWechat && isCopy) &&
        <button className="python-button python-button__study">已复制链接，快去粘贴发给好友吧～</button>
      }

      {
        entryMode !== 0 &&
        <button className="python-button python-button__study">我也要学Python</button>
      }
    </div>
  );
}

export default PythonClass;