import { Routes, UrlMatchResult, UrlSegment } from "@angular/router";
import { AVAILABLE_LOCALES } from "@peertube/peertube-core-utils";
import { MetaGuard } from "./core";
import { EmptyComponent } from "./empty.component";
import { HomepageRedirectComponent } from "./homepage-redirect.component";
import { USER_USERNAME_REGEX_CHARACTERS } from "./shared/form-validators/user-validators";
import { ActorRedirectGuard } from "./shared/shared-main/router/actor-redirect-guard.service";
import { VideosParentComponent } from "./videos-parent.component";

const routes: Routes = [
  {
    path: "admin",
    loadChildren: () => import("./+admin/routes"),
    canActivateChild: [MetaGuard],
  },

  // ---------------------------------------------------------------------------

  {
    path: "my-account",
    loadChildren: () => import("./+my-account/routes"),
    canActivateChild: [MetaGuard],
  },
  {
    path: "my-library",
    loadChildren: () => import("./+my-library/routes"),
    canActivateChild: [MetaGuard],
  },
  {
    path: "verify-account",
    loadChildren: () => import("./+signup/+verify-account/routes"),
    canActivateChild: [MetaGuard],
  },

  {
    path: "accounts",
    redirectTo: "a",
  },
  {
    path: "a",
    loadChildren: () => import("./+accounts/routes"),
    canActivateChild: [MetaGuard],
  },

  // ---------------------------------------------------------------------------

  {
    path: "video-channels",
    redirectTo: "c",
  },
  {
    path: "c",
    loadChildren: () => import("./+video-channels/routes"),
    canActivateChild: [MetaGuard],
  },

  {
    path: "manage/create",
    redirectTo: "/my-library/video-channels/create",
    pathMatch: "full",
  },

  {
    path: "manage/update/:channel",
    pathMatch: "full",
    redirectTo: "/my-library/video-channels/update/:channel",
  },

  // ---------------------------------------------------------------------------

  {
    path: "p",
    loadChildren: () => import("./shared/shared-plugin-pages/routes"),
    canActivateChild: [MetaGuard],
    data: {
      parentRoute: "/",
    },
  },

  {
    path: "about",
    loadChildren: () => import("./+about/routes"),
    canActivateChild: [MetaGuard],
  },
  {
    path: "signup",
    loadChildren: () => import("./+signup/+register/routes"),
    canActivateChild: [MetaGuard],
  },
  {
    path: "reset-password",
    loadChildren: () => import("./+reset-password/routes"),
    canActivateChild: [MetaGuard],
  },
  {
    path: "login",
    loadChildren: () => import("./+login/routes"),
    canActivateChild: [MetaGuard],
  },
  {
    path: "search",
    loadChildren: () => import("./+search/routes"),
    canActivateChild: [MetaGuard],
  },

  // ---------------------------------------------------------------------------

  {
    path: "studio/edit/:videoId",
    redirectTo: "/videos/manage/:videoId/studio",
    pathMatch: "full",
  },

  {
    path: "stats/videos/:videoId",
    redirectTo: "/videos/manage/:videoId/stats",
    pathMatch: "full",
  },

  {
    path: "videos/upload",
    redirectTo: "/videos/publish",
    pathMatch: "full",
  },
  {
    path: "videos/update/:uuid",
    pathMatch: "full",
    redirectTo: "/videos/manage/:uuid",
  },

  {
    path: "videos/manage/:uuid",
    loadChildren: () => import("./+videos-publish-manage/+video-manage/routes"),
    canActivateChild: [MetaGuard],
    data: {
      meta: {
        title: $localize`Manage your video`,
      },
    },
  },

  {
    path: "videos/publish",
    loadChildren: () =>
      import("./+videos-publish-manage/+video-publish/routes"),
    canActivateChild: [MetaGuard],
    data: {
      meta: {
        title: $localize`Publish your video`,
      },
    },
  },

  // ---------------------------------------------------------------------------

  {
    path: "video-playlists/watch",
    redirectTo: "w/p",
  },

  {
    path: "videos/watch/playlist",
    redirectTo: "w/p",
  },
  {
    path: "videos/watch",
    redirectTo: "w",
  },
  {
    path: "w",
    redirectTo: "",
  },

  // ---------------------------------------------------------------------------
  // /home and other /videos routes
  // ---------------------------------------------------------------------------
  {
    matcher: (url): UrlMatchResult => {
      if (url.length < 1) return null;

      const matchResult =
        url[0].path === "home" ||
        url[0].path === "videos" ||
        url[0].path === "events-hub" ||
        url[0].path === "event-hub-controller" ||
        url[0].path === "manage-event"||
        url[0].path === "live-stream-via-solace"||
        url[0].path === "manage-live"||
        url[0].path === "admin-controller" ||
        url[0].path === "live";
      if (!matchResult) return null;

      // So the children can detect the appropriate route
      return { consumed: [] };
    },
    component: VideosParentComponent,
    children: [
      // {
      //   path: "home",
      //   loadChildren: () => import("./+home/routes"),
      //   canActivateChild: [MetaGuard],
      // },
      {
        path: "home",
        loadChildren: () => import("./+video-list/routes"),
        canActivateChild: [MetaGuard],
      },
      {
        path: 'event-hub-controller',
        loadChildren: () => import('./+event-hub/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Event Hub Controller`
          }
        }
      },
      {
        path: 'events-hub',
        loadChildren: () => import('./+events/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Events Catalog`
          }
        }
      },
      {
        path: 'manage-event',
        loadChildren: () => import('./+manage-event/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Manage Event`
          }
        }
      },
      {
        path: 'manage-event/:id',
        loadChildren: () => import('./+manage-event/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Edit Event`
          }
        }
      },
      {
        path: 'manage-live',
        loadChildren: () => import('./+admin-live/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Manage Event`
          }
        }
      },
      {
        path: 'manage-live/:id',
        loadChildren: () => import('./+admin-live/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Edit Event`
          }
        }
      },
      {
        path: 'admin-controller',
        loadChildren: () => import('./+admin-controller/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Admin`
          }
        }
      },
      {
        path: 'live',
        loadChildren: () => import('./+live-stream/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Live`
          }
        }
      },
      {
        path: 'live-stream-via-solace',
        loadChildren: () => import('./+live-stream2/routes'),
        canActivateChild: [ MetaGuard ],
        data: {
          meta: {
            title: $localize`Live Stream via Solace`
          }
        }
      },
    ],
  },

  // ---------------------------------------------------------------------------

  {
    path: "remote-interaction",
    loadChildren: () => import("./+remote-interaction/routes"),
    canActivateChild: [MetaGuard],
  },

  // ---------------------------------------------------------------------------
  // /@:actorName
  // ---------------------------------------------------------------------------
  {
    matcher: (url): UrlMatchResult => {
      const regex = new RegExp(`^@(${USER_USERNAME_REGEX_CHARACTERS}+)$`);
      if (url.length !== 1) return null;

      const matchResult = url[0].path.match(regex);
      if (!matchResult) return null;

      return {
        consumed: url,
        posParams: {
          actorName: new UrlSegment(matchResult[1], {}),
        },
      };
    },
    pathMatch: "full",
    canActivate: [ActorRedirectGuard],
    component: EmptyComponent,
  },

  // ---------------------------------------------------------------------------

  {
    path: "",
    component: HomepageRedirectComponent,
  },
];

// Avoid 404 when changing language
for (const locale of AVAILABLE_LOCALES) {
  routes.push({
    path: locale,
    component: HomepageRedirectComponent,
  });
}

routes.push({
  path: "**",
  loadChildren: () => import("./+error-page/routes"),
});

export default routes;
