import { Layout } from '@arco-design/web-react'
import { WorkspacePage } from './workspace'
import { ProjectChannelPage } from './projects/project-channel-page'
import { ProjectRail } from './projects/project-rail'
import { ProjectSidebar } from './projects/project-sidebar'
import { ProjectStatusbar } from './projects/project-statusbar'
import { ProjectTopbar } from './projects/project-topbar'
import type { ChannelKey, Project, ProjectWorkspace, Theme, WorkbenchView } from './projects/project-types'

const { Content } = Layout

export function ProjectPage({
  activeView,
  activeWorkspace,
  workspaces,
  activeChannel,
  theme,
  onSelectProject,
  onSelectWorkspace,
  onSelectChannel,
  onSelectItem,
  onToggleTheme,
}: {
  activeView: WorkbenchView
  activeWorkspace: ProjectWorkspace
  workspaces: ProjectWorkspace[]
  activeChannel: ChannelKey
  theme: Theme
  onSelectProject: (project: Project) => void
  onSelectWorkspace: () => void
  onSelectChannel: (key: ChannelKey) => void
  onSelectItem: (title: string) => void
  onToggleTheme: () => void
}) {
  const isWorkspace = activeView === 'workspace'
  const projects = workspaces.map((workspace) => workspace.project)

  return (
    <Layout className="workbench-shell">
      <ProjectTopbar theme={theme} onToggleTheme={onToggleTheme} />

      <Layout className="workbench-main" hasSider>
        <ProjectRail
          activeProject={activeWorkspace.project}
          projects={projects}
          activeView={activeView}
          onSelectWorkspace={onSelectWorkspace}
          onSelectProject={onSelectProject}
        />

        {!isWorkspace && (
          <ProjectSidebar
            activeView={activeView}
            activeWorkspace={activeWorkspace}
            activeChannel={activeChannel}
            onSelectChannel={onSelectChannel}
            onSelectItem={onSelectItem}
          />
        )}

        <Content className="stage">
          {isWorkspace ? (
            <WorkspacePage workspaces={workspaces} onSelectProject={onSelectProject} />
          ) : (
            <ProjectChannelPage
              activeChannel={activeChannel}
              activeWorkspace={activeWorkspace}
              onSelectItem={onSelectItem}
            />
          )}
        </Content>

      </Layout>

      <ProjectStatusbar />
    </Layout>
  )
}
