import * as React from 'react';
import { Props, State } from './type';
import { isLofter, isUpAppVersion } from 'nw-detect';
import { callHandler } from 'nejsbridge/dist/bridge.lofter.es';
import { BaseComponent } from '../base-component'
import { showMessage } from '../common/toast/toast'
import { isWeixin } from 'nw-detect';

// 全局发布器
const GLOBAL_PUBLISH = 'lofter://www.lofter.com/post/create/'
// 讨论/说说发布器
const TAGCHAT_PUBLISH = 'lofter://www.lofter.com/tagChat/create/'
// app 升级页面
const APP_VERSION_UPGRADE = 'https://www.lofter.com/front/homesite/version-upgrade'

export class HotAreaPublish extends React.Component<Props, State> {
  public static defaultProps = new Props();
  public state = new State();

  handleClick = () => {
    const { publishType } = this.props;
    const { tag = '', content = '', title = '', chatId, chatTitle } = publishType.data || {};
    let link = TAGCHAT_PUBLISH
    if (publishType.type === 0) {
      callHandler('njb_publish')
      return;
    }
    if (isWeixin()) {
      return window.location.href = APP_VERSION_UPGRADE;
    }
    switch (publishType.type) {
      case 1:
        link += '?publish=photo'
        break;
      case 2:
        link += '?publish=video'
        break;
      case 3:
        link += '?publish=text'
        break;
      default:
        break;
    }
    if (publishType.type !== 4) {
      if (isLofter() && !isUpAppVersion('7.1.4')) {
        showMessage('请更新版本到7.1.4以上')
        return
      }
      link += `&tag=${encodeURIComponent(tag)}&content=${encodeURIComponent(content)}&title=${encodeURIComponent(title)}`
    } else {
      if (isLofter() && !isUpAppVersion('7.4.0')) {
        showMessage('请更新版本到7.4.0以上')
        return
      }
      link += `?${chatId ? `chatId=${encodeURIComponent(chatId)}` : ''}${chatTitle ? `&chatTitle=${encodeURIComponent(chatTitle)}` : ''}`
    }
    window.location.href = link;
  }

  render() {
    const { style, backgroundImage } = this.props;

    return (
      <div
        style={{
          ...style,
        }}
        onClick={this.handleClick.bind(this)}
      >
        <BaseComponent {...this.props}>
          {backgroundImage && <img style={this.props.imgStyle} src={this.props.backgroundImage.url}/>}
        </BaseComponent>
      </div>
    )
  }
}