import Vue from "vue";
import Router from "vue-router";
import * as namespace from "../view/namespace.js";

// import BookCity from '../view/book-city.vue'
// import BookStock from '../view/book-stock.vue'
// import BookShelf from '../view/book-shelf.vue'
// import BookRecent from '../view/book-recent.vue'
// import BookDetail from '../view/book-detail.vue'
// import BookCatalog from '../view/book-catalog.vue'
// import Pay from '../view/pay.vue'
// import Rank from '../view/rank.vue'
// import Person from '../view/person.vue'
// import Reader from '../view/reader.vue'
// import Subscribe from '../view/subscribe.vue'
// import RecordOrder from '../view/record-order.vue'
// import RecordOrderBook from '../view/record-order-book.vue'
// import RecordRecharge from '../view/record-recharge.vue'
// import Undertheshelf from '../view/under-the-shelf.vue'
const BookCity = () => import("../view/book-city.vue");
const BookStock = () => import("../view/book-stock.vue");
const BookShelf = () => import("../view/book-shelf.vue");
const BookRecent = () => import("../view/book-recent.vue");
const BookDetail = () => import("../view/book-detail.vue");
const BookCatalog = () => import("../view/book-catalog.vue");
const BookRecommend = () => import("../view/book-recommend.vue");
const Pay = () => import("../view/pay.vue");
const Help = () => import("../view/help.vue");
const Rank = () => import("../view/rank.vue");
const Rank2 = () => import("../view/rank_2.vue");
const Person = () => import("../view/person.vue");
const Reader = () => import("../view/reader.vue");
const Subscribe = () => import("../view/subscribe.vue");
const RecordOrder = () => import("../view/record-order.vue");
const RecordOrderBook = () => import("../view/record-order-book.vue");
const RecordRecharge = () => import("../view/record-recharge.vue");
const RecordGift = () => import("../view/record-gift.vue");
const RecordSign = () => import("../view/record-sign.vue");
const Undertheshelf = () => import("../view/under-the-shelf.vue");
const HelpPayQrcode = () => import("../view/help-pay-qrcode.vue");
const HelpPay = () => import("../view/help-pay.vue");
const BookShare = () => import("../view/book-share.vue");
const HelpShareQrcode = () => import("../view/help-share-qrcode.vue");
const Report = () => import("../view/report-page.vue");
const PhoneBinding = () => import("../view/phone-binding.vue");
const FakeBookDetail = () => import("../view/fake-book-detail.vue");
const RecordSend = () => import("../view/record-send.vue");
const Sign = () => import("../view/sign.vue");
const BookLimitFree = () => import("../view/book-limit-free.vue");
const Coupon = () => import("../view/my_coupon.vue");
// import test from '../view/test.vue'

Vue.use(Router);

/**
 routes:[
   [namespace,components,children]
 ]
 children:[
    [namespace,components,children]
 ]
 */
function generateRoute(routes) {
  return routes.map(val => {
    const route = {
      path: val[0].path,
      name: val[0].name,
      component: val[1],
    };
    if (val[0].meta) route.meta = val[0].meta;
    if (val[0].redirect) route.redirect = val[0].redirect;
    if (val[0].alias) route.alias = val[0].alias;
    if (val[2]) route.children = generateRoute(val[2]);
    return route;
  });
}

let behavior = "user";

addEventListener("popstate", function() {
  //我的事件要在vue-router事件之前绑定
  behavior = "browser";
});

const router = new Router({
  mode: "history",
  routes: generateRoute([
    [namespace.Pay, Pay],
    [namespace.Rank, Rank2],
    // [namespace.Help, Help,
    [namespace.Reader, Reader],
    [namespace.Person, Person],
    [namespace.Subscribe, Subscribe],
    [namespace.BookCity, BookCity],
    [namespace.BookStock, BookStock],
    [namespace.BookShelf, BookShelf],
    [namespace.BookRecent, BookRecent],
    [namespace.BookDetail, BookDetail],
    [namespace.BookCatalog, BookCatalog],
    [namespace.BookRecommend, BookRecommend],
    [namespace.RecordOrder, RecordOrder],
    [namespace.RecordOrderBook, RecordOrderBook],
    [namespace.RecordGift, RecordGift],
    [namespace.RecordRecharge, RecordRecharge],
    [namespace.RecordSign, RecordSign],
    [namespace.RecordSend, RecordSend],
    [namespace.Undertheshelf, Undertheshelf],
    [namespace.Sign, Sign],
    [namespace.Coupon,Coupon],
    [namespace.HelpPayQrcode, HelpPayQrcode],
    [namespace.HelpPay, HelpPay],
    [namespace.BookShare, BookShare],
    [namespace.HelpShareQrcode, HelpShareQrcode],
    [namespace.Report, Report],
    [namespace.PhoneBinding, PhoneBinding],
    [namespace.FakeBookDetail, FakeBookDetail],
    [namespace.BookLimitFree, BookLimitFree]
  ]),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  }
});

router.beforeEach((to, from, next) => {
  window.prevPage = from.fullPath;
  window.nextPage = to.fullPath;
  to.meta ? (to.meta.behavior = behavior) : (to.meta = { behavior });
  behavior = "user";
  next();
});

router.afterEach(function(router, from) {
  if (process.env.NODE_ENV !== "development") {
    window.MtaH5.pgv();
    window._hmt.push(["_trackPageview", router.fullPath + "#" + from.name]);
  }
});

export default router;
