mirrored_repositories/Dr-Health/lib/src/components
// ignore_for_file: deprecated_member_use

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:intl/intl.dart';
import 'package:dr_health/src/database/repository.dart';
import 'package:dr_health/src/models/pill.dart';
import 'package:dr_health/src/handlers/notifications_handler.dart';

class MedicineCard extends StatelessWidget {

  final Pill medicine;
  final Function setData;
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  MedicineCard(this.medicine,this.setData,this.flutterLocalNotificationsPlugin);

  @override
  Widget build(BuildContext context) {
    //check if the medicine time is lower than actual
    final bool isEnd = DateTime.now().millisecondsSinceEpoch > medicine.time;

    return Card(
      shadowColor: Colors.greenAccent.shade200,
      elevation: 8.0,
      margin: EdgeInsets.symmetric(vertical: 7.0),
      color: Theme.of(context).primaryColor.withOpacity(1.0), //Colors.white,
      child: ListTile(
        shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
        onLongPress: () =>
            _showDeleteDialog(context, medicine.name, medicine.id, medicine.notifyId),
        contentPadding:
            EdgeInsets.symmetric(vertical: 15.0, horizontal: 15.0),
        title: Text(
          medicine.name,
          style: Theme.of(context).textTheme.headline1.copyWith(
              color: Colors.white70,
              fontSize: 20.0,
              decoration: isEnd ? TextDecoration.lineThrough : null),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        subtitle: Text(
          "${medicine.description}" + "\n" + "${medicine.amount} ${medicine.medicineForm}",
          style: Theme.of(context).textTheme.headline5.copyWith(
              color: Colors.white70,
              fontSize: 15.0,
              fontWeight: FontWeight.w500,
              decoration: isEnd ? TextDecoration.lineThrough : null),
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
        ),
        isThreeLine: true,
        trailing: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Text(
              DateFormat("HH:mm").format(
                  DateTime.fromMillisecondsSinceEpoch(medicine.time)),
              style: TextStyle(
                  color: Colors.white70,
                  fontWeight: FontWeight.w500,
                  fontSize: 15,
                  decoration: isEnd ? TextDecoration.lineThrough : null),
            ),
          ],
        ),
        leading: Container(
          width: 60.0,
          height: 60.0,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(50.0),
            child: ColorFiltered(
                colorFilter: ColorFilter.mode(
                    isEnd ? Colors.white : Colors.transparent,
                    BlendMode.saturation),
                child: Image.asset(
                  medicine.image
                )),
          ),
        ),
      ),
    );
  }


  //--------------------------| SHOW THE DELETE DIALOG ON THE SCREEN |-----------------------

  void _showDeleteDialog(BuildContext context, String medicineName, int medicineId, int notifyId) {
    showDialog(
        context: context,
        builder: (context) => AlertDialog(
              title: Text("Delete ?"),
              content: Text("Are you sure to delete $medicineName medicine?"),
              contentTextStyle:
                  TextStyle(fontSize: 17.0, color: Colors.grey[800]),
              actions: [
                FlatButton(
                  splashColor: Theme.of(context).primaryColor.withOpacity(0.3),
                  child: Text(
                    "Cancel",
                    style: TextStyle(color: Theme.of(context).primaryColor),
                  ),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                FlatButton(
                  splashColor: Theme.of(context).primaryColor.withOpacity(0.3),
                  child: Text("Delete",
                      style: TextStyle(color: Theme.of(context).primaryColor)),
                  onPressed: () async {
                    await Repository().deleteData('Pills', medicineId);
                    await Notifications().removeNotify(notifyId, flutterLocalNotificationsPlugin);
                    setData();
                    Navigator.pop(context);
                  },
                ),
              ],
            ));
  }
  //============================================================================================


}
