const wx2my = require('../wx2my');
const Behavior = require('../Behavior');
class Router {
  constructor(url, switchTab) {
    const switchPages = [// '/pages/page/page',
      // '/pages/nav/nav',
      // '/pages/cart/cart',
      // '/pages/userCenter/userCenter'
    ];
    this.router = url || '';
    this.navigateType = switchTab ? 'switchTab' : 'navigate';

    function getRouter(router) {
      return router.split('?')[0];
    }

    this.navigateManage = {
      navigate: () => {
        if (switchPages.indexOf(getRouter(this.router)) === -1) {
          wx2my.navigateTo({
            url: this.router
          });
        } else {
          wx2my.switchTab({
            url: getRouter(this.router)
          });
        }
      },
      switchTab: () => {
        wx2my.switchTab({
          url: getRouter(this.router)
        });
      },
      back: () => {
        wx2my.navigateBack({
          delta: 1
        });
      },
      redirect: () => {
        if (switchPages.indexOf(getRouter(this.router)) === -1) {
          wx2my.redirectTo({
            url: this.router
          });
        } else {
          wx2my.switchTab({
            url: getRouter(this.router)
          });
        }
      }
    };
    setTimeout(() => {
      const navigateType = this.navigateType;
      this.navigateManage[navigateType]();
    }, 0);
  }

  search(parmas) {
    var temp = [];

    for (let p in parmas) {
      temp.push(`${p}=${parmas[p]}`);
    }

    this.router += `?${temp.join('&')}`;
    console.log('this.router', this.router)
    return this;
  }

  replace() {
    this.navigateType = 'redirect';
    return this;
  }

  switchTab() {
    this.navigateType = 'switchTab';
    return this;
  }

  back(dintance) {
    this.navigateType = 'back';
    return this;
  }

}

export default Router;