import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.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/itg_forms_models/fields_model.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';

class RequestDetailFragment extends StatelessWidget {
  List<Fields> fields;

  RequestDetailFragment({Key? key, this.fields = const <Fields>[]}) : super(key: key);
  double itemHeight = 0;
  double itemWidth = 0;
  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    itemHeight = (size.height - kToolbarHeight - 24) / 9;
    itemWidth = size.width / 2;
    List<Widget> uiList = [detailView()];
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: fields.isEmpty
          ? LocaleKeys.noDataAvailable.tr().toText16().center
          : ListView(
              padding: const EdgeInsets.all(21),
              children: uiList,
            ),
    );
  }

  Widget detailView() {
    bool isOdd = false;
    if (fields.length % 2 != 0) {
      isOdd = true;
      fields.add(new Fields());
    }
    return GridView.builder(
      itemCount: fields.length,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        if (fields[index].value == null) {
          return ItemDetailViewGridItem(
            index,
            fields[index].title, fields[index].multipleValue?.join(", ") ?? "",
            isNeedToShowEmptyDivider: (fields.length == index + 1)
                ? isOdd
                ? true
                : false
                : false,
            type: fields[index].type,
          );
        }
        return ItemDetailViewGridItem(
          index,
          fields[index].title,
          fields[index].value ?? "",
          isNeedToShowEmptyDivider: (fields.length == index + 1)
              ? isOdd
              ? true
              : false
              : false,
          type: fields[index].type,
        );
      },
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: (itemWidth / itemHeight),
      ),
    ).objectContainerView();
    return ListView.separated(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (cxt, index) {
              if (fields[index].value == null) {
                return ItemDetailView(fields[index].title!, fields[index].multipleValue?.join(", ") ?? "");
              }
              return ItemDetailView(fields[index].title!, fields[index].value ?? "");
            },
            separatorBuilder: (cxt, index) => 4.height,
            itemCount: fields.length)
        .objectContainerView();
  }
}
