import { PluginBase } from '../PluginBase';
import { forceLogin, tryLogin } from '../../../plugins/user';

export class UserLoginDemo extends PluginBase {
  public getPluginShortDesc(): JSX.Element {
    return (
      <div>用户登录</div>
    );
  }

  public getPluginFullDesc(): JSX.Element {
    return (
      <div>
        <div>用户强制登陆，或者尝试登陆，返回用户userInfo</div>
      </div>
    );
  }
  private viewCode = (mdPath) => () => {
    this.showMarkdownPopup(require(`./${mdPath}.md`));
  }
  public state = {
    value: {},
  };

  private forceLogin = () => {
    forceLogin().then(result => {
      this.setState({
        value: result,
      });
    }).catch(err => {
      alert(JSON.stringify(err));
    });
  }
  private tryLogin = () => {
    tryLogin().then(result => {
      this.setState({
        value: result,
      });
    }).catch(err => {
      alert(JSON.stringify(err));
    });
  }
  public render() {
    return (
      <div className="plugin-page">
        <h2 className="left">{this.props.pluginName}</h2>
        <div className="short-desc">{this.getPluginShortDesc()}</div>
        <div className="full-desc">{this.getPluginFullDesc()}</div>
        <div className="text-area">{JSON.stringify(this.state.value)}</div>
        <div className="body">
          <div className="play-body-item">
            <button className="exec" onClick={this.forceLogin}>强制登陆</button>
            <button className="view" onClick={this.viewCode('force-login')}>查看代码</button>
          </div>
          <div className="play-body-item">
            <button className="exec" onClick={this.tryLogin}>尝试登陆</button>
            <button className="view" onClick={this.viewCode('force-login')}>查看代码</button>
          </div>
        </div>
      </div>
    );
  }
}
