import { message } from 'ant-design-vue'
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { ELocalStorageKey, ERouterName, EUserType } from '/@/types'
import CreatePlan from '/@/components/task/CreatePlan.vue'
import WaylinePanel from '/@/pages/page-web/projects/wayline.vue'
import DockPanel from '/@/pages/page-web/projects/dock.vue'
import { apis, SignLoginApiParams } from '/@/api/custom/index'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/' + ERouterName.PROJECT
  },
  {
    path: '/' + ERouterName.PROJECT,
    name: ERouterName.PROJECT,
    component: () => import('/@/pages/page-web/index.vue')
  },
  {
    path: '/' + ERouterName.HOME,
    name: ERouterName.HOME,
    component: () => import('/@/pages/page-web/home.vue'),
    children: [
      {
        path: '/' + ERouterName.DEVICES,
        name: ERouterName.DEVICES,
        component: () => import('/@/pages/page-web/projects/devices.vue')
      },
      {
        path: '/' + ERouterName.WORKSPACE,
        name: ERouterName.WORKSPACE,
        component: () => import('/@/pages/page-web/projects/workspace.vue'),
      },
      {
        path: '/' + ERouterName.TASK,
        name: ERouterName.TASK,
        component: () => import('/@/pages/page-web/projects/task/index.vue')
      },
      {
        path: '/' + ERouterName.MEDIA,
        name: ERouterName.MEDIA,
        component: () => import('/@/pages/page-web/projects/media/index/index.vue')
      },
      {
        path: '/' + ERouterName.MEDIA + '/:id',
        name: ERouterName.MEDIA + '/:id',
        component: () => import('/@/pages/page-web/projects/media/detail/index.vue')
      },
      {
        path: '/' + ERouterName.TRAJECTORY,
        name: ERouterName.TRAJECTORY,
        component: () => import('/@/pages/page-web/projects/trajectory/index.vue')
      },
      {
        path: '/' + ERouterName.MEMBER,
        name: ERouterName.MEMBER,
        component: () => import('/@/pages/page-web/projects/member/index.vue')
      },
    ]
  },
  {
    path: '/mediaInfo',
    component: () => import('/@/pages/page-web/projects/mediaInfo/index.vue')
  },
  {
    path: '/' + ERouterName.PILOT,
    name: ERouterName.PILOT,
    component: () => import('/@/pages/page-pilot/pilot-index.vue'),
  },
  {
    path: '/' + ERouterName.PILOT_HOME,
    component: () => import('/@/pages/page-pilot/pilot-home.vue')
  },
  {
    path: '/' + ERouterName.PILOT_MEDIA,
    component: () => import('/@/pages/page-pilot/pilot-media.vue')
  },
  {
    path: '/' + ERouterName.PILOT_LIVESHARE,
    component: () => import('/@/pages/page-pilot/pilot-liveshare.vue')
  },
  {
    path: '/' + ERouterName.PILOT_BIND,
    component: () => import('/@/pages/page-pilot/pilot-bind.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

const updateToken = async (data: SignLoginApiParams) => {
  try {
    const result = await apis.signLogin(data);
    if (result.code === 0) {
      localStorage.setItem(ELocalStorageKey.Token, result.data.access_token)
      localStorage.setItem(ELocalStorageKey.WorkspaceId, result.data.workspace_id)
      localStorage.setItem(ELocalStorageKey.Username, result.data.username)
      localStorage.setItem(ELocalStorageKey.UserId, result.data.user_id)
      localStorage.setItem(ELocalStorageKey.Flag, EUserType.Web.toString())
    } else {
      message.error(result.message)
    }
  } catch (e) {
    console.error(e);
  }
}

router.beforeEach(async (to, from, next) => {
  const { username, client_id, timestamp, workspace_id, workspace_name, signature } = to.query as any;
  if (username && client_id && timestamp && workspace_id && workspace_name && signature) {
    localStorage.clear()
    await updateToken({ username, client_id, timestamp, workspace_id, workspace_name, signature })
    next();
  } else {
    next();
  }
})

export default router