/// <reference path="./index.d.ts" />

import * as React from 'react';
import { hashHistory, browserHistory } from 'react-router';
import assign from 'object-assign';
import EventTracking from '../event-tracking';
import init from './init';
import config from './config';
import Global from './global';
import View from '../view';
import ListView from '../list-view';
import WebApi from '../ajax/webapi';
import Request from './request';
import WeiXin from './weixin';
import * as router from './router';

class Framework<P, S> extends React.Component<P, S> {
  constructor(props: P, state: S) {
    super(props, state);

    this.historyPush = this.historyPush.bind(this);
  }

  // tslint:disable-next-line:typedef
  public static history = hashHistory;
  /** 常规配置 */
  public static config: TspFrameworkConfig = config;
  /** 全局变量 */
  public static global: Global = new Global();
  /** 接口请求对象 */
  public static request: Request;
  /** 微信接口 */
  public static wx: WeiXin;
  /** react-router的routes属性 */
  public static routes: object;
  /** 路由 */
  // tslint:disable-next-line:typedef
  public router = {
    goBack: router.goBack,
    push: this.historyPush
  };
  /** 行为记录实例对象 */
  public eventTracking: EventTracking = EventTracking.api && EventTracking.host && EventTracking.request ? new EventTracking() : undefined;

  /** 初始化 */
  public static init(options: TspFrameworkInitOptions): void {
    Framework.config = assign({}, config, options.config);
    Framework.request = new Request(options.request);
    if (options.router && options.router.history) {
      Framework.history = options.router.history === 'browserHistory' ? browserHistory : hashHistory;
    }
    Framework.routes = router.init(options.router);

    if (options.wx) {
      Framework.wx = new WeiXin(options.wx);
    }

    init(options, Framework.request);
  }

  /** router-router的push方法封装 */
  public historyPush(route: string): void {
    if (this.eventTracking) {
      this.eventTracking.setRecordEventTracking(true);
    }
    EventTracking.isRecordEventTracking = true;
    Framework.history.push(route);
  }
}

export default Framework;