import React, { useState } from 'react';
import {
  SimpleGrid,
  Card,
  Text,
  ActionIcon,
  Group,
  Stack,
  Title,
  Badge,
} from '@mantine/core';
import { useNavigate } from 'react-router-dom';
import {
  IconStar,
  IconStarFilled,
  IconDashboard,
  IconChartLine,
  IconSettings,
  IconUsers,
  IconFiles,
  IconMail,
} from '@tabler/icons-react';
import Breadcrumbs from '../components/Breadcrumbs';

interface App {
  id: string;
  name: string;
  description: string;
  icon: React.ComponentType<any>;
  path: string;
  category: string;
}

const HomePage: React.FC = () => {
  const navigate = useNavigate();
  const [favoriteApps, setFavoriteApps] = useState<string[]>(['demo', 'analytics']);

  const availableApps: App[] = [
    {
      id: 'demo',
      name: 'Demo App',
      description: 'Sample application for testing',
      icon: IconDashboard,
      path: '/app/demo',
      category: 'Development',
    },
    {
      id: 'analytics',
      name: 'Analytics',
      description: 'Data analytics and reporting',
      icon: IconChartLine,
      path: '/app/analytics',
      category: 'Analytics',
    },
    {
      id: 'settings',
      name: 'Settings',
      description: 'System configuration',
      icon: IconSettings,
      path: '/app/settings',
      category: 'System',
    },
    {
      id: 'users',
      name: 'User Management',
      description: 'Manage users and permissions',
      icon: IconUsers,
      path: '/app/users',
      category: 'Administration',
    },
    {
      id: 'files',
      name: 'File Manager',
      description: 'Browse and manage files',
      icon: IconFiles,
      path: '/app/files',
      category: 'Utilities',
    },
    {
      id: 'mail',
      name: 'Mail Client',
      description: 'Email management',
      icon: IconMail,
      path: '/app/mail',
      category: 'Communication',
    },
  ];

  const toggleFavorite = (appId: string) => {
    setFavoriteApps(prev =>
      prev.includes(appId)
        ? prev.filter(id => id !== appId)
        : [...prev, appId]
    );
  };

  const favoriteAppsList = availableApps.filter(app => favoriteApps.includes(app.id));
  const otherApps = availableApps.filter(app => !favoriteApps.includes(app.id));

  const AppTile: React.FC<{ app: App }> = ({ app }) => {
    const IconComponent = app.icon;
    
    return (
      <Card
        shadow="sm"
        padding="lg"
        radius="md"
        withBorder
        style={{ cursor: 'pointer', height: '100%' }}
        onClick={() => navigate(app.path)}
      >
        <Group justify="space-between" align="flex-start" mb="xs">
          <IconComponent size={32} color="var(--mantine-color-blue-6)" />
          <ActionIcon
            variant="subtle"
            onClick={(e) => {
              e.stopPropagation();
              toggleFavorite(app.id);
            }}
          >
            {favoriteApps.includes(app.id) ? (
              <IconStarFilled size={16} color="gold" />
            ) : (
              <IconStar size={16} />
            )}
          </ActionIcon>
        </Group>

        <Stack gap="xs">
          <Text fw={500} size="lg">
            {app.name}
          </Text>
          <Text size="sm" c="dimmed">
            {app.description}
          </Text>
          <Badge variant="light" size="sm">
            {app.category}
          </Badge>
        </Stack>
      </Card>
    );
  };

  return (
    <Stack gap="xl">
      <Breadcrumbs />
      
      {favoriteAppsList.length > 0 && (
        <div>
          <Group mb="md">
            <IconStarFilled size={20} color="gold" />
            <Title order={2}>Favorite Applications</Title>
          </Group>
          <SimpleGrid cols={{ base: 1, sm: 2, md: 3, lg: 4 }} spacing="md">
            {favoriteAppsList.map(app => (
              <AppTile key={app.id} app={app} />
            ))}
          </SimpleGrid>
        </div>
      )}

      <div>
        <Title order={2} mb="md">
          All Applications
        </Title>
        <SimpleGrid cols={{ base: 1, sm: 2, md: 3, lg: 4 }} spacing="md">
          {otherApps.map(app => (
            <AppTile key={app.id} app={app} />
          ))}
        </SimpleGrid>
      </div>
    </Stack>
  );
};

export default HomePage;