// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:farm_tpf/presentation/custom_widgets/checkbox/checkbox_widget.dart';
import 'package:farm_tpf/utils/helpers.dart';
import 'package:flutter/material.dart';

import 'package:farm_tpf/presentation/screens/codes/models/stamp.dart';

import '../../../../themes/app_colors.dart';
import '../../../../themes/styles_text.dart';
import 'package:farm_tpf/utils/formatter.dart';

import '../models/task.dart';

class ItemTask extends StatelessWidget {
  final Task item;
  final Function onPressed;
  final Function(bool) onChangedStatus;
  ItemTask({
    Key? key,
    required this.item,
    required this.onPressed,
    required this.onChangedStatus,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var dueDate = item.dueDate?.format_DDMMYY().toString() ?? '';
    var completedDate = item.executeDate?.format_DDMMYY().toString() ?? '';
    var backgroundColor = Colors.white;
    if (item.isCompleted ?? false) {
      backgroundColor = AppColors.primary1.withOpacity(0.3);
    } else {
      var dueDateCompare = item.dueDate?.convertStringServerDateTimeToLocalDateTime() ?? DateTime.now();
      if (Helpers.isAfterToday(dueDateCompare)) {
        backgroundColor = Colors.white;
      } else {
        backgroundColor = AppColors.semantic6;
      }
    }
    return GestureDetector(
      onTap: () {
        onPressed();
      },
      child: Container(
        padding: EdgeInsets.all(8),
        margin: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          border: Border.all(
            color: Colors.grey.shade200,
            width: 1,
          ),
          color: backgroundColor,
        ),
        child: Row(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '${item.title ?? ''}',
                    style: StylesText.body4,
                  ),
                  const SizedBox(
                    height: 4,
                  ),
                  Text(
                    'Hạn chót: $dueDate',
                    style: StylesText.body6,
                  ),
                  const SizedBox(
                    height: 4,
                  ),
                  Text(
                    'Hoàn thành: $completedDate',
                    style: StylesText.body6,
                  ),
                ],
              ),
            ),
            CheckboxWidget(
              title: '',
              isChecked: item.isCompleted ?? false,
              onChange: (val) {
                onChangedStatus(val);
              },
            )
          ],
        ),
      ),
    );
  }
}
