import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store/index'

Vue.use(VueRouter);

const ifNotAuthenticated = (to, from, next) => {
  if (!store.getters.isAuthenticated) {

      next();
      return;
  }
  next("/");
}

const ifAuthenticated = (to, from, next) => {
  if (store.getters.isAuthenticated) {
      next();
      return
  }
  next('/login')
}



const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    beforeEnter: ifNotAuthenticated
  },
  {
    path: '/esqueci-minha-senha',
    name: 'Esqueci minha senha',
    component: () => import('../views/password/Forgot.vue'),
    beforeEnter: ifNotAuthenticated
  },
  {
    path: '/nova-senha/:token/:email',
    name: 'Nova Senha',
    component:  () => import('../views/password/NewPassword.vue'),
    props: { default: true, sidebar: false },
    beforeEnter: ifNotAuthenticated
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/tour',
    name: 'Tour',
    component: () => import('../views/tour/index.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/produtos',
    name: 'Todos os Produtos',
    component: () => import('../views/Products.vue'),
    meta: {
      description: 'Você encontrará todos os produtos disponíveis. Valores válidos para o campanha: {{CAMPAIGN}}.',
    },
    beforeEnter: ifAuthenticated
  },
  {
    path: '/financeiro',
    name: 'Financeiro',
    component: () => import('../views/Finance.vue'),
    meta: {
      description: 'Você encontrará todos os lançamentos financeiros.',
    },
    beforeEnter: ifAuthenticated
  },
  {
    path: '/pedidos',
    name: 'Pedidos',
    component: () => import('../views/order/index.vue'),
    children: [
      {
        name: 'Meus Pedidos',
        path: "meus-pedidos",
        component: () => import('../views/order/List.vue'),
              meta: {
                description: 'Você encontrará o historico dos seus pedidos.',
              },

      },
      {
        name: 'Digitar Pedido',
        path: "digitar-pedido",
        component: () => import('../views/order/Create.vue'),
        meta: {
          description: 'Nesta tela você faz seus pedidos.',
        },
      },
      {
        name: 'Checkout',
        path: "checkout",
        component: () => import('../views/order/Checkout.vue'),
        meta: {
          description: 'Nesta tela você realiza o pagamento dos seus pedidos. Valores válidos para o campanha: {{CAMPAIGN}}.',
        },
      }
    ],
    beforeEnter: ifAuthenticated
  },
  {
    path: '/perfil',
    name: 'Meu Perfil',
    component: () => import('../views/Profile.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/consultas-spc',
    name: 'Consultas SPC',
    component: () => import('../views/Consults.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/solicitacoes-cadastro',
    name: 'Solicitações de cadastro',
    component: () => import('../views/Solicitations.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/usuarios',
    name: 'Usuários',
    component: () => import('../views/User.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/banner',
    name: 'Banner',
    component: () => import('../views/Banner.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/promocoes',
    name: 'Promoções',
    component: () => import('../views/Promotion.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/promocoes-desconto-progressivo',
    name: 'Promoções Desconto Progressivo',
    component: () => import('../views/PromotionProgressiveDiscount.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/material-de-apoio',
    name: 'Material de Apoio',
    component: () => import('../views/SupportMaterial.vue'),
    beforeEnter: ifAuthenticated
  },
  {// Está como produtos em destaque
    path: '/produtos-mais-vendidos',
    name: 'Produtos Mais Vendidos',
    component: () => import('../views/FeaturedProducts.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/material-extra',
    name: 'Materiais Extras',
    component: () => import('../views/ExtraMaterials.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/treinamento',
    name: 'Treinamento',
    component: () => import('../views/VideoMaterials.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/campanhas',
    name: 'Campanhas',
    component: () => import('../views/Campaign.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/last-time-offers',
    name: 'Offers',
    component: () => import('../views/LastTimeOffers.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/consultoras',
    name: 'Consultoras',
    component: () => import('../views/SalesConultant.vue'),
    beforeEnter: ifAuthenticated
  },
  {
    path: '/cadastro',
    name: 'Cadastro de Revendedora',
    component: () => import('../views/signup/index.vue'),
    meta: {
      description: 'Cadastre suas consultoras',
    },
    children: [
      {
        name: 'Documentos',
        path: 'cpf',
        component: () => import('../views/signup/FirstInfo.vue'),
      },
      {
        name: 'Dados Pessoais',
        path: 'dados',
        component: () => import('../views/signup/PersonalData.vue'),
        props: true
      },
      {
        name: 'Contatos',
        path: 'contatos',
        component: () => import('../views/signup/Contacts.vue'),
        props: true
      },
      {
        name: 'Endereço de Cobrança',
        path: 'endereco-de-cobranca',
        component: () => import('../views/signup/Address.vue'),
        props: true
      },
      {
        name: 'Endereço de Entrega',
        path: 'endereco-de-entrega',
        component: () => import('../views/signup/AddressDelivery.vue'),
        props: true
      },
      {
        name: 'Complemento',
        path: 'complemento',
        component: () => import('../views/signup/AddressDetails.vue'),
        props: true
      },
      {
        name: 'Cadastro Concluido',
        path: 'cadastro-concluido',
        component: () => import('../views/signup/CompletedSignup.vue'),
      },
    ],
    beforeEnter: ifAuthenticated
  },
  {
    path: '/verificar-celular/:cpf',
    name: 'Verificação de Celular',
    component: () => import('../views/signup/VerifyCellphone.vue'),
    beforeEnter: ifNotAuthenticated
  },
  {
    path: '/pre-cadastro',
    name: 'Pre-Cadastro',
    component: () => import('../views/selfSignUp/index.vue'),
    meta: {
      description: 'Pré-Cadastro',
    },
    children: [
      {
        path: "lista",
        name: "Lista Pre-Cadastro",
        component: () => import('../views/selfSignUp/list.vue'),
        beforeEnter: ifAuthenticated,
      },
      {
        path: '',
        name: 'Cadastro de Consultora',
        component: () => import('../views/selfSignUp/create.vue'),
        meta: {
          description: 'Faça seu cadastro',
        },
        beforeEnter: ifNotAuthenticated,
      }
      ],
    },

  {
    path: '/obrigado-por-se-registrar',
    name: 'Cadastro de Revendedora',
    component: () => import('../views/selfSignUp/thankyou.vue'),
    meta: {
      description: 'Obrigado por se registrar',
    },
    beforeEnter: ifNotAuthenticated
  },
  {
    name: 'Em Desenvolvimento',
    path: '/em-desenvolvimento',
    component: () => import('../views/TempPage.vue'),
  },
  {
    name: 'pageNotFound',
    path: '*',
    component: () => import('../views/PageNotFound.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

/*
router.beforeEach((to, from, next) => {

  if ((to.name === "Login" || to.name === "Verificação de Celular") && store.getters.isAuthenticated) {
    next("/");

  } else if (!store.getters.isAuthenticated && (to.name === "Login" || to.name === "Verificação de Celular")) {
    next();
  } else if(!store.getters.isAuthenticated && (to.name !== "Login" || to.path !== "Verificação de Celular")) {
    next("/login");
  }else{
    next();
  }
});
*/

export default router;
