import 'package:flutter/material.dart';

import '../../../../../../common/function/app_function.dart';
import '../../../../../../common/theme/theme.dart';
import '../../../../../../domain/table/table.dart' as t;
import '../../../../../../domain/table/table.dart';

class TableCard extends StatelessWidget {
  final t.Table table;
  final bool isSelected;
  const TableCard({super.key, required this.table, required this.isSelected});

  Color getStatusColor() {
    switch (table.status) {
      case TableStatusType.available:
        return Colors.blue[100]!;
      case TableStatusType.occupied:
        return Colors.orange[100]!;
      default:
        return Colors.grey[200]!;
    }
  }

  Color getBorderColor() {
    if (isSelected) return AppColor.primary;
    switch (table.status) {
      case TableStatusType.available:
        return Colors.blue;
      case TableStatusType.occupied:
        return Colors.orange;
      default:
        return Colors.grey;
    }
  }

  @override
  Widget build(BuildContext context) {
    final int capacity = table.capacity;
    final chairDist = getChairDistribution(capacity);

    Widget chair() => Container(
      width: 20,
      height: 10,
      decoration: BoxDecoration(
        color: Colors.grey[300],
        borderRadius: BorderRadius.circular(4),
      ),
    );

    return SizedBox(
      width: table.capacity > 16
          ? 240
          : table.capacity > 8
          ? 180
          : 120,
      height: 80,
      child: Stack(
        alignment: Alignment.center,
        children: [
          Container(
            width: table.capacity > 16
                ? 220
                : table.capacity > 8
                ? 160
                : 100,
            height: 60,
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border.all(color: getBorderColor(), width: 2),
              borderRadius: BorderRadius.circular(16),
            ),
            child: Center(
              child: CircleAvatar(
                radius: 24,
                backgroundColor: getStatusColor(),
                child: Text(
                  table.tableName,
                  style: TextStyle(
                    color: getBorderColor(),
                    fontWeight: FontWeight.bold,
                    fontSize: 18,
                  ),
                ),
              ),
            ),
          ),

          if (chairDist['top']! > 0)
            Positioned(
              top: 0,
              left: 10,
              right: 10,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: List.generate(chairDist['top']!, (_) => chair()),
              ),
            ),
          // Kursi bawah
          if (chairDist['bottom']! > 0)
            Positioned(
              bottom: 0,
              left: 10,
              right: 10,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: List.generate(chairDist['bottom']!, (_) => chair()),
              ),
            ),
          // Kursi kiri
          if (chairDist['left']! > 0)
            Positioned(
              left: 0,
              top: 15,
              bottom: 15,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: List.generate(chairDist['left']!, (_) => chair()),
              ),
            ),
          // Kursi kanan
          if (chairDist['right']! > 0)
            Positioned(
              right: 0,
              top: 15,
              bottom: 15,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: List.generate(chairDist['right']!, (_) => chair()),
              ),
            ),
          // Icon info kecil di pojok kanan atas jika status reserved
          if (table.status.isOccupied)
            const Positioned(
              top: 6,
              right: 6,
              child: Icon(
                Icons.info_outline,
                size: 16,
                color: Colors.redAccent,
              ),
            ),
        ],
      ),
    );
  }
}
