import 'package:flutter/material.dart';
import 'package:on_field_work/controller/permission_controller.dart';
import 'package:on_field_work/controller/task_Planning/daily_task_Planning_controller.dart';
import 'package:on_field_work/helpers/widgets/my_text.dart';

class DailyTaskPlanningFilter extends StatelessWidget {
  final DailyTaskPlanningController controller;
  final PermissionController permissionController;

  const DailyTaskPlanningFilter({
    super.key,
    required this.controller,
    required this.permissionController,
  });

  @override
  Widget build(BuildContext context) {
    String? tempSelectedProjectId = '654563563645';
    bool showProjectList = false;

    final accessibleProjects = controller.projects
        .where((project) =>
            permissionController.isUserAssignedToProject(project.id.toString()))
        .toList();

    return StatefulBuilder(builder: (context, setState) {
      List<Widget> filterWidgets;

      if (showProjectList) {
        filterWidgets = accessibleProjects.isEmpty
            ? [
                Padding(
                  padding: EdgeInsets.all(12.0),
                  child: Center(
                    child: MyText.titleSmall(
                      'No Projects Assigned',
                      fontWeight: 600,
                    ),
                  ),
                ),
              ]
            : accessibleProjects.map((project) {
                final isSelected =
                    tempSelectedProjectId == project.id.toString();
                return ListTile(
                  dense: true,
                  contentPadding: const EdgeInsets.symmetric(horizontal: 16),
                  title: MyText.titleSmall(project.name),
                  trailing: isSelected ? const Icon(Icons.check) : null,
                  onTap: () {
                    setState(() {
                      tempSelectedProjectId = project.id.toString();
                      showProjectList = false;
                    });
                  },
                );
              }).toList();
      } else {
        final selectedProject = accessibleProjects.isNotEmpty
            ? accessibleProjects.firstWhere(
                (p) => p.id.toString() == tempSelectedProjectId,
                orElse: () => accessibleProjects[0],
              )
            : null;

        final selectedProjectName = selectedProject?.name ?? "Select Project";

        filterWidgets = [
          Padding(
            padding: EdgeInsets.fromLTRB(16, 12, 16, 4),
            child: Align(
              alignment: Alignment.centerLeft,
              child: MyText.titleSmall(
                'Select Project',
                fontWeight: 600,
              ),
            ),
          ),
          ListTile(
            dense: true,
            contentPadding: const EdgeInsets.symmetric(horizontal: 16),
            title: MyText.titleSmall(selectedProjectName),
            trailing: const Icon(Icons.arrow_drop_down),
            onTap: () => setState(() => showProjectList = true),
          ),
        ];
      }

      return Padding(
        padding: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom,
        ),
        child: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.only(top: 12, bottom: 8),
                child: Center(
                  child: Container(
                    width: 40,
                    height: 4,
                    decoration: BoxDecoration(
                      color: Colors.grey[400],
                      borderRadius: BorderRadius.circular(4),
                    ),
                  ),
                ),
              ),
              ...filterWidgets,
              const Divider(),
              Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
                child: SizedBox(
                  width: double.infinity,
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Color.fromARGB(255, 95, 132, 255),
                      padding: const EdgeInsets.symmetric(vertical: 12),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: MyText.titleSmall(
                      'Apply Filter',
                      fontWeight: 600,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      Navigator.pop(context, {
                        'projectId': tempSelectedProjectId,
                      });
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    });
  }
}
