import Vue from "vue";
import Router from "vue-router";
import util from "@/common/utils.js";

Vue.use(Router);

const routes = [
  {
    path: "/common",
    name: "common",
    component: () => import("./components/common/_common.vue"),
    children: [
      {
        path: "payment-agreement",
        name: "paymentAgreement",
        component: () => import("./components/common/PaymentAgreement.vue")
      },
      {
        path: "service-agreement",
        name: "serviceAgreement",
        component: () => import("./components/common/ServiceAgreement.vue")
      },
      {
        path: "user-agreement",
        name: "userAgreement",
        component: () => import("./components/common/UserAgreement.vue")
      },
      {
        path: "404",
        name: "NotFound",
        component: () => import("./components/common/NotFound.vue")
      }
    ]
  },
  {
    path: "/csp",
    name: "csp",
    component: () => import("./components/csp.vue")
  },
  {
    path: "/:lang",
    name: "lang",
    component: () => import("./components/common/_lang.vue"),
    children: [
      {
        path: "csp",
        name: "cspLang",
        component: () => import("./components/csp.vue")
      },
      {
        path: "bootPage",
        name: "bootPage",
        component: () => import("./components/bootPage.vue")
      },
      {
        path: "guide",
        name: "guide",
        component: () => import("./components/guide.vue")
      },
      {
        path: "house-polymer/:id",
        name: "housePolymer",
        component: () => import("./components/common/housePolymer.vue")
      },
      {
        path: "article/:articleId",
        name: "article",
        component: () => import("./components/article.vue")
      },
      {
        path: "guideForm",
        name: "guideForm",
        component: () => import("./components/guideForm.vue")
      },
      {
        path: "form",
        name: "form",
        component: () => import("./components/form.vue")
      },
      {
        path: "areaCode",
        name: "AreaCode",
        component: () => import("./components/common/AreaCode.vue")
      },
      {
        path: "service",
        name: "service",
        component: () => import("./components/service.vue")
      },
      {
        path: "service-article/:id",
        name: "serviceArticle",
        component: () => import("./components/common/ServiceArticle.vue")
      },
      {
        path: "messages-center",
        name: "messagesCenter",
        component: () => import("./components/messagesCenter.vue")
      },
      {
        path: "messages",
        name: "messages",
        component: () => import("./components/messages.vue")
      },
      {
        path: "validatePay",
        name: "validatePay",
        component: () => import("./components/validatePay.vue")
      },
      {
        path: "pay",
        name: "pay",
        component: () => import("./components/pay.vue")
      },
      {
        path: "orderInfo/:id",
        name: "orderInfo",
        component: () => import("./components/orderInfo.vue")
      },
      {
        path: "refund",
        name: "refund",
        component: () => import("./components/refund.vue")
      },
      {
        path: "serial-number",
        name: "serialNumber",
        component: () => import("./components/serialNumber.vue")
      },
      {
        path: "cashier",
        name: "cashier",
        component: () => import("./components/cashier.vue")
      },
      {
        path: "login",
        name: "login",
        component: () => import("./components/login.vue")
      },
      {
        path: "register",
        name: "register",
        component: () => import("./components/register.vue")
      },
      {
        path: "verificationCode",
        name: "verificationCode",
        component: () => import("./components/verificationCode.vue")
      },
      {
        path: "setNickname",
        name: "setNickname",
        component: () => import("./components/setNickname.vue")
      },
      {
        path: "smsLogin",
        name: "smsLogin",
        component: () => import("./components/smsLogin.vue")
      },
      {
        path: "bind",
        name: "bind",
        component: () => import("./components/bind.vue")
      },
      {
        path: "forget",
        name: "forget",
        component: () => import("./components/forget.vue")
      },
      {
        path: "success",
        name: "success",
        component: () => import("./components/common/success.vue")
      },
      {
        path: "error",
        name: "error",
        component: () => import("./components/common/error.vue")
      },
      {
        path: "download",
        name: "download",
        component: () => import("./components/common/download.vue")
      },
      {
        path: "user",
        name: "user",
        component: () => import("./components/user.vue"),
        children: [
          {
            path: "about",
            name: "about",
            component: () => import("./components/user/about.vue")
          },
          {
            path: "card",
            name: "card",
            component: () => import("./components/user/card.vue")
          },
          {
            path: "verificationCard",
            name: "verificationCard",
            component: () => import("./components/user/verificationCard.vue")
          },
          {
            path: "account",
            name: "account",
            component: () => import("./components/user/account.vue")
          },
          {
            path: "attentions",
            name: "attentions",
            component: () => import("./components/user/attentions.vue")
          },
          {
            path: "agents",
            name: "agents",
            component: () => import("./components/user/agents.vue")
          },
          {
            path: "information",
            name: "information",
            component: () => import("./components/user/information.vue")
          },
          {
            path: "orders",
            name: "orders",
            component: () => import("./components/user/orders.vue")
          },
          {
            path: "orderDetail/:id",
            name: "orderDetail",
            component: () => import("./components/user/orderDetail.vue")
          },
          {
            path: "apply",
            name: "apply",
            component: () => import("./components/user/apply.vue")
          },
          {
            path: "applyDetail/:id",
            name: "applyDetail",
            component: () => import("./components/user/applyDetail.vue")
          },
          {
            path: "coupons",
            name: "coupons",
            component: () => import("./components/user/coupons.vue")
          },
          {
            path: "editMobile",
            name: "editMobile",
            component: () => import("./components/user/editMobile.vue")
          },
          {
            path: "editMail",
            name: "editMail",
            component: () => import("./components/user/editMail.vue")
          },
          {
            path: "editPassword",
            name: "editPassword",
            component: () => import("./components/user/editPassword.vue")
          }
        ]
      },
      {
        path: "building/:id",
        name: "building",
        component: () => import("./components/building.vue"),
        children: [
          {
            path: "rooms",
            name: "rooms",
            component: () => import("./components/common/RoomList.vue")
          }
        ]
      },
      {
        path: "room/:id",
        name: "room",
        component: () => import("./components/room.vue")
      },
      {
        path: "applyForm",
        name: "applyForm",
        component: () => import("./components/applyForm.vue")
      },
      {
        path: "agent/:id",
        name: "agent",
        component: () => import("./components/agent.vue")
      },
      {
        path: ":cityTag",
        component: () => import("./components/common/_cityTag.vue"),
        children: [
          {
            path: "/",
            name: "home",
            component: () =>
              import(/* webpackChunkName: "home" */ "./views/home.vue")
          },
          {
            path: "buildings",
            name: "buildings",
            component: () => import("./components/buildings.vue")
          },
          {
            path: "filter",
            name: "filter",
            component: () => import("./components/filter.vue")
          },
          {
            path: "search",
            name: "search",
            component: () => import("./components/search.vue")
          }
        ]
      }
    ]
  },
  {
    path: "/",
    redirect: { name: "home" }
  }
];

const router = new Router({
  mode: "history",
  base: "/",
  routes
});

router.beforeEach((to, from, next) => {
  if (typeof window !== "undefined") {
    let host = window.location.host;
    if (host === "m.hoolihome.com") {
      if (
        !/Android|iPhone|Windows Phone|BlackBerry|iPod|Kindle/i.test(
          window.navigator.userAgent
        )
      ) {
        let redirectUrl = `//www.hoolihome.com${to.fullPath}`;
        window.location.replace(redirectUrl);
      }
    }
    if (to.name === "login" || to.name === "verificationCode") {
      util.sessionStorage.save("backHome", true);
    } else {
      if (
        from.name !== "login" &&
        from.name !== "verificationCode" &&
        from.name
      ) {
        util.sessionStorage.remove("backHome");
      }
    }
    next();
  }
});
export default router;
