import 'package:cheminova/models/select_task_response.dart';
import 'package:cheminova/provider/select_task_provider.dart';
import 'package:flutter/material.dart';
import 'package:cheminova/screens/collect_kyc_screen.dart';
import 'package:cheminova/widgets/common_app_bar.dart';
import 'package:cheminova/widgets/common_drawer.dart';
import 'package:cheminova/widgets/common_background.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

class SelectTaskkycScreen extends StatefulWidget {
  const SelectTaskkycScreen({super.key});

  @override
  State<SelectTaskkycScreen> createState() => SelectTaskkycScreenState();
}

class SelectTaskkycScreenState extends State<SelectTaskkycScreen> {
  late SelectTaskProvider _selectTaskProvider;

  @override
  void initState() {
    _selectTaskProvider = SelectTaskProvider(); // Initialize the SelectTaskProvider
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => _selectTaskProvider, // Provide the SelectTaskProvider
      child: CommonBackground(
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: CommonAppBar(
            title: const Text('Select Task'), // Title for the app bar
            backgroundColor: Colors.transparent,
            elevation: 0,
            actions: [
              IconButton(
                onPressed: () => Navigator.pop(context), // Back button functionality
                icon: Image.asset('assets/Back_attendance.png'),
                padding: const EdgeInsets.only(right: 20),
              ),
            ],
          ),
          drawer: const CommonDrawer(), // Common navigation drawer
          body: Consumer<SelectTaskProvider>(
            builder: (context, value, child) =>
            value.isLoading
                ? const Center(child: CircularProgressIndicator()) // Loading indicator while tasks are fetched
                : _buildTaskList(), // Build the task list once loading is complete
          ),
        ),
      ),
    );
  }

  // Method to build the list of tasks
  Widget _buildTaskList() {
    return Consumer<SelectTaskProvider>(
      builder: (context, value, child) {
        if (value.tasksList.isEmpty) {
          return const Center(
            child: Text(
              'NO TASK', // Message displayed if no tasks are available
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: Colors.black87,
              ),
            ),
          );
        }
        return ListView.builder(
          itemCount: value.tasksList.length, // Number of tasks to display
          itemBuilder: (context, index) => _buildTaskCard(value.tasksList[index]), // Build each task card
        );
      },
    );
  }

  // Method to build an individual task card
  Widget _buildTaskCard(Tasks task) {
    return InkWell(
      onTap: () => Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => CollectKycScreen(id: task.sId ?? ''), // Navigate to CollectKycScreen
        ),
      ),
      child: Card(
        margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), // Margin around the card
        color: Colors.white,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ListTile(
              leading: const Icon(Icons.task, color: Colors.blueAccent), // Task icon
              title: Text(
                task.task ?? '', // Task title
                style: const TextStyle(
                  color: Colors.black87,
                  fontWeight: FontWeight.w700,
                  fontSize: 16,
                  fontFamily: 'Anek',
                ),
              ),
              trailing: const Icon(Icons.arrow_forward_ios, color: Colors.black87), // Arrow icon for navigation
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('Note: ${task.note}'), // Display task note
                  if (task.taskDueDate != null)
                    Text(
                      'Date: ${task.taskDueDate == null ? '' : DateFormat('dd/MM/yyyy').format(DateTime.parse(task.taskDueDate ?? ''))}', // Formatted due date
                    ),
                  Text('Priority: ${task.taskPriority}'), // Display task priority
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

// Base class for task items
class TaskItem {
  final String title; // Title of the task
  final Widget screen; // Screen to navigate to

  TaskItem({required this.title, required this.screen});
}

// Extended class for KYC task items with additional attributes
class KycTaskItem extends TaskItem {
  final String note; // Note associated with the task
  final String date; // Due date of the task
  final String priority; // Priority of the task

  KycTaskItem({
    required String title,
    required Widget screen,
    required this.note,
    required this.date,
    required this.priority,
  }) : super(title: title, screen: screen);
}
