import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/profile_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.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/performance.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';

class PerformanceAppraisal extends StatelessWidget {
  PerformanceAppraisal({Key? key}) : super(key: key);

  Future<List<GetPerformanceAppraisalList>> getPerformanceAppraisal(context) async {
    List<GetPerformanceAppraisalList> performance = [];
    try {
      Utils.showLoading(context);
      performance = await ProfileApiClient().getPerformanceAppraisal();
      Utils.hideLoading(context);
    } catch (ex) {
      performance = [];
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
    return performance;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.backgroundColor,
      appBar: AppBarWidget(context, title: LocaleKeys.performance.tr()),
      body: FutureBuilder(
        future: getPerformanceAppraisal(context),
        builder: (cxt, snapShot) {
          if (snapShot.connectionState == ConnectionState.done) {
            if (snapShot.hasData) {
              List<GetPerformanceAppraisalList> performance = snapShot.data as List<GetPerformanceAppraisalList>;
              if (performance.isNotEmpty) {
                return ListView(
                  padding: const EdgeInsets.all(0),
                  children: [
                    getPerformanceCard(performance.first).paddingOnly(left: 21, right: 21, top: 21),
                    GridView.count(
                      physics: const NeverScrollableScrollPhysics(),
                      crossAxisCount: 2,
                      mainAxisSpacing: 13,
                      crossAxisSpacing: 13,
                      shrinkWrap: true,
                      childAspectRatio: 160 / 125,
                      padding: const EdgeInsets.only(left: 21, right: 21, bottom: 21, top: 14),
                      children: performance
                          .where((GetPerformanceAppraisalList el) => performance.indexOf(el) != 0)
                          .map(
                            (GetPerformanceAppraisalList item) => getPerformanceCard(item, isGrid: true),
                          )
                          .toList(),
                    ),
                  ],
                );
              }
            }
            return Utils.getNoDataWidget(context);
          }
          return const SizedBox();
        },
      ),
    );
  }

  Widget getPerformanceCard(GetPerformanceAppraisalList performance, {bool isGrid = false}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: const EdgeInsets.only(left: 12, right: 12, top: 2, bottom: 2),
          decoration: Utils.containerRadius(MyColors.greenColor, 5),
          child: "Excellent".toText10(color: Colors.white),
        ),
        (isGrid ? 8 : 4).height,
        isGrid ? "${LocaleKeys.performanceEvaluationIn.tr()} ${performance.aPPRAISALYEAR}".toText13() : "${LocaleKeys.performanceEvaluationIn.tr()} ${performance.aPPRAISALYEAR}".toText18(),
        4.height,
        LinearProgressIndicator(
          minHeight: 4,
          backgroundColor: MyColors.lightGreyDeColor,
          valueColor: const AlwaysStoppedAnimation<Color>(MyColors.greenColor),
          value: double.parse(performance.aPPRAISALSCORE!) / 100,
        ),
        4.height,
        isGrid ? "${performance.aPPRAISALSCORE}%".toText13(color: MyColors.greenColor) : "${performance.aPPRAISALSCORE}%".toText20(color: MyColors.greenColor),
      ],
    ).objectContainerView(radius: 10);
  }
}
