import 'package:flutter/material.dart';
import 'package:mobile_portal_23/models/reportListModel.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:badges/badges.dart' as badges;
import 'package:mobile_portal_23/classes/common_classes.dart';

import 'package:mobile_portal_23/widgets/reportDetails.dart';

class reportCardWidget extends StatefulWidget {
  // reportCardWidget({Key? key}) : super(key: key);

  final ModelReportList reportListItem;
  const reportCardWidget(this.reportListItem, {super.key});
  @override
  State<reportCardWidget> createState() => _reportCardWidgetState();
}

List<String> favoriteWorks = [];
//late List<ModelReportList> reportList = [];
bool _showFaworites = false;
late EmployeePageArguments employeePageArguments;
Future<void> _saveFavoriteWorks(List<String> favoriteWorks) async {
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setStringList('favoriteWorks', favoriteWorks);
}

Future<void> getSharedPrefs() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  // userApiKey = prefs.getString("apiKey");
  favoriteWorks = prefs.getStringList("favoriteWorks") ?? [];
}

class _reportCardWidgetState extends State<reportCardWidget> {
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final args = ModalRoute.of(context)!.settings.arguments as Map;

      employeePageArguments = args['userArgs'];
    });
    getSharedPrefs();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return reportCard(context, widget.reportListItem);
  }

  Card reportCard(BuildContext context, ModelReportList item) {
    int workHour = 0;
    bool workOpened = false;
    bool isFaworite = favoriteWorks.contains(item.sId);
    if (item.state!.contains("Opened")) workOpened = true;
    for (var element in item.dailyReport) {
      workHour += element.employeeList.length;
    }
    return Card(
      clipBehavior: Clip.antiAlias,
      child: Column(
        children: [
          badges.Badge(
            showBadge: !workOpened,
            position: badges.BadgePosition.topEnd(top: 3, end: 3),
            badgeContent: const Icon(Icons.lock_outlined),
            child: ListTile(
              tileColor: workOpened ? Colors.blue : Colors.grey,
              trailing: IconButton(
                  onPressed: () {
                    if (isFaworite) {
                      favoriteWorks.remove(item.sId);
                    } else {
                      String? id = item.sId;
                      favoriteWorks.add(id!);
                    }
                    _saveFavoriteWorks(favoriteWorks);
                    setState(() {});
                  },
                  icon: isFaworite
                      ? Icon(
                    Icons.star,
                    color: favoriteWorks.contains(item.sId)
                        ? Colors.yellow
                        : null,
                  )
                      : const Icon(Icons.star_border)),
              /* Icon(
              Icons.keyboard_arrow_right,
              // size: 48,
              color: Colors.white,

            ),*/
              onTap: () {
                ReportArguments reportArgs = ReportArguments(
                  item.dailyReport,
                  //userApiKey!,
                  item.title!,
                  item.sId!,
                  item.state == "Opened" ? true : false,
                  //employeePageArguments.isForeman!
                );
                Navigator.pushNamed(context, reportDetails.routeName,
                    arguments: {
                      'userArgs': employeePageArguments,
                      'dataArgs': reportArgs
                    });
              },
              leading: CircleAvatar(
                  backgroundColor: Colors.white,
                  child: Text(
                    item.dailyReport.length.toString(),
                    style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: Colors.blue),
                  )),
              title: Text(
                item.title!,
                style: const TextStyle(color: Colors.white),
              ),
              subtitle: Text(
                item.body!,
                style: TextStyle(color: Colors.black.withOpacity(0.6)),
              ),
            ),
          ),
          Padding(
              padding: const EdgeInsets.all(10.0),
              child: IntrinsicHeight(
                  child: Column(children: [
                    Row(children: [
                      Text(
                        'KL${item.workNumber!}',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            color: Colors.black.withOpacity(0.6)),
                      ),
                      const VerticalDivider(color: Colors.grey, thickness: 1, width: 30),
                      Text(
                        'Órák: ${workHour * 8} h',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            color: Colors.black.withOpacity(0.6)),
                      ),
                      const VerticalDivider(color: Colors.grey, thickness: 1, width: 30),
                      Text(
                        'PO:${item.poNumber!}',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            color: Colors.black.withOpacity(0.6)),
                      ),
                    ]),
                    const Divider(
                      thickness: 1.0,
                    ),
                    Row(
                      children: [
                        Flexible(
                          child: Text(
                            item.dailyReport[0].workTitle!,
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Colors.black.withOpacity(0.6)),
                          ),
                        )
                      ],
                    ),
                    const Divider(
                      thickness: 1.0,
                    ),
                  ]))),
          /* ButtonBar(
          alignment: MainAxisAlignment.start,
          children: [
            ElevatedButton.icon(
                onPressed: () {
                  // Perform some action
                  ReportArguments arg = new ReportArguments(
                      reportList[index].dailyReport, userApiKey!);
                  Navigator.pushNamed(context, reportDetails.routeName,
                      arguments: arg);
                },
                icon: const Icon(Icons.mode_edit),
                label: const Text('Megnyitás')),
          ],
        ),*/
          //Image.asset('assets/card-sample-image-2.jpg'),
        ],
      ),
    );
  }
}
