import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/vacation_rule_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/vacation_rule/get_vacation_rules_list_model.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';

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

  @override
  _VacationRuleScreenState createState() {
    return _VacationRuleScreenState();
  }
}

class _VacationRuleScreenState extends State<VacationRuleScreen> {
  List<GetVacationRulesList>? vacationRuleList;

  @override
  void initState() {
    super.initState();
    getVacationRulesList();
  }

  void getVacationRulesList() async {
    try {
      Utils.showLoading(context);
      vacationRuleList = await VacationRuleApiClient().getVacationRules();
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(
        context,
        title: LocaleKeys.vacationRule.tr(),
      ),
      body: vacationRuleList == null
          ? const SizedBox()
          : (vacationRuleList!.isEmpty
              ? Utils.getNoDataWidget(context)
              : ListView.separated(
                  physics: const BouncingScrollPhysics(),
                  padding: const EdgeInsets.all(21),
                  itemBuilder: (cxt, int parentIndex) => Container(
                        width: double.infinity,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10),
                          boxShadow: [
                            BoxShadow(
                              color: const Color(0xff000000).withOpacity(.05),
                              blurRadius: 26,
                              offset: const Offset(0, -3),
                            ),
                          ],
                        ),
                        clipBehavior: Clip.antiAlias,
                        child: Stack(
                          clipBehavior: Clip.antiAlias,
                          children: [
                            Positioned(
                              left: -20,
                              top: -10,
                              child: Transform.rotate(
                                angle: 15,
                                child: Container(
                                  width: 50,
                                  height: 30,
                                  color: getStatusColor(vacationRuleList![parentIndex].rULESTATUS!),
                                ),
                              ),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                ("${vacationRuleList![parentIndex].rULENAME!} | ${vacationRuleList![parentIndex].iTEMTYPEDISPLAYNAME!}").toText16(),
                                4.height,
                                ItemDetailView(LocaleKeys.startDateT.tr(), getParsedTime(vacationRuleList![parentIndex].bEGINDATE!)),
                                ItemDetailView(LocaleKeys.endDateT.tr(), getParsedTime(vacationRuleList![parentIndex].eNDDATE!)),
                                4.height,
                                vacationRuleList![parentIndex].rULESTATUS!.toText12(color: getStatusColor(vacationRuleList![parentIndex].rULESTATUS!)),
                              ],
                            ).paddingOnly(top: 16, left: 16, right: 16, bottom: 16),
                          ],
                        ),
                      ),
                  separatorBuilder: (cxt, index) => 12.height,
                  itemCount: vacationRuleList!.length)),
      floatingActionButton: Container(
        height: 54,
        width: 54,
        decoration: const BoxDecoration(
          shape: BoxShape.circle,
          gradient: LinearGradient(transform: GradientRotation(.83), begin: Alignment.topRight, end: Alignment.bottomLeft, colors: [
            MyColors.gradiantEndColor,
            MyColors.gradiantStartColor,
          ]),
        ),
        child: const Icon(Icons.add, color: Colors.white, size: 30),
      ).onPress(() {
        Navigator.pushNamed(context, AppRoutes.addVacationRule);
      }),
    );
  }

  Color getStatusColor(String code) {
    if (code == "SUBMIT") {
      return const Color(0xff2E303A);
    } else if (code == "Inactive") {
      return const Color(0xffD02127);
    } else if (code == "active") {
      return const Color(0xff1FA269);
    } else if (code == "REQUEST_INFO") {
      return const Color(0xff2E303A);
    } else {
      return const Color(0xff2E303A);
    }
  }

  String getParsedTime(String time) {
    DateTime date = DateFormat("MM/dd/yyyy").parse(time);
    return DateFormat("d MMM yyyy").format(date);
  }
}
