import 'dart:convert';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/colors.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/itg_worklist_table_model.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheet.dart';

class ItemDetailView extends StatelessWidget {
  final String title;
  final String value;

  const ItemDetailView(this.title, this.value, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        "$title:".toText12(isBold: true, color: const Color(0xff2D3238)),
        6.width,
        (value.isEmpty ? "--" : value).toText12(color: MyColors.normalTextColor).expanded,
      ],
    );
  }
}

class ItemDetailViewCol extends StatelessWidget {
  final String title;
  final String value;

  const ItemDetailViewCol(this.title, this.value, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        "$title:".toText12(isBold: true, color: const Color(0xff2BB8A6), maxLine: 2),
        4.width,
        Flexible(child: (value.isEmpty ? "--" : value).toText12(color: MyColors.normalTextColor, maxLine: null)),
      ],
    );
  }
}

class ItemDetailViewGridItem extends StatelessWidget {
  int index;
  final String? title;
  final String? value;
  final String? type;
  final bool isNeedToShowEmptyDivider;
  final int maxLine;
  final bool showSpaceAfterLine;

  ItemDetailViewGridItem(this.index, this.title, this.value, {Key? key, this.isNeedToShowEmptyDivider = false, this.showSpaceAfterLine = false, this.type = "", this.maxLine = 6}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: index % 2 != 0 ? 12 : 0),
      decoration: BoxDecoration(
        color: Colors.transparent,
        border: Border(
          left: BorderSide(
            //MyColors.lightGreyEFColor
            //                   <--- left side
            color: index % 2 != 0 ? MyColors.lightGreyEFColor : Colors.transparent,
            width: 1.5,
          ),
          top: BorderSide(
            //                   <--- left side
            color: index > 1 ? MyColors.lightGreyEFColor : Colors.transparent,
            width: 1.5,
          ),
        ),
      ),
      child: isNeedToShowEmptyDivider
          ? SizedBox()
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                title != null ? Flexible(child: "$title:".toText12(isBold: true, color: const Color(0xff2BB8A6))) : Container(),
                4.width,
                     type != null
                      ? type!.toLowerCase() == "table"
                          ? getStringFromJSON(value!)
                          // : Flexible(child: (value!.isEmpty ? "--" : value).toString().toText12Auto(color: MyColors.normalTextColor, maxLine: 5))
                          : (value!.isEmpty ? "--" : value).toString().toText12(color: MyColors.normalTextColor, maxLine:  2)
                      : Container(),
                if (value!.length > 100)
                  GestureDetector(
                    onTap: () {
                      showModalBottomSheet(
                        context: context,
                        enableDrag: true,
                        isDismissible: true,
                        shape: const RoundedRectangleBorder(
                          borderRadius: BorderRadius.only(topLeft: Radius.circular(12), topRight: Radius.circular(12)),
                        ),
                        backgroundColor: Colors.white,
                        builder: (context) {
                          return openWorkListBottomSheet(context, value, title);
                        },
                      );

                    },
                    child: Padding(
                      padding: const EdgeInsets.only(top: 4),
                      child: Text(
                        LocaleKeys.showMore.tr(),
                        textAlign: TextAlign.right,
                        style: const TextStyle(
                          color: Color(0xff2BB8A6),
                          fontSize: 12,
                          fontWeight: FontWeight.w600,

                        ),
                      ),
                    ),
                  ),
              ],

            ).paddingOnly(top: showSpaceAfterLine ? 16 : 0, left: 2, right: 2),
    );
  }

}

class ItemDetailGrid extends StatelessWidget {
  Widget child1, child2;

  bool isItLast;

  ItemDetailGrid(this.child1, this.child2, {this.isItLast = false});

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        IntrinsicHeight(
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Expanded(
                flex: 1,
                child: Padding(
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  child: child1,
                ),
              ),
              Container(
                width: 1,
                height: double.infinity,
                color: MyColors.lightGreyEFColor,
                margin: EdgeInsets.symmetric(horizontal: 8),
              ),
              Expanded(
                flex: 1,
                child: Padding(
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  child: child2,
                ),
              ),
            ],
          ),
        ),
        if (!isItLast) 1.divider,
      ],
    );
  }
}

Widget getStringFromJSON(String jsonString) {
  var body = json.decode(jsonString);
  ITGWorkListTableModel itgWorkListTableModel = ITGWorkListTableModel();
  if (body.length != 0) {
    itgWorkListTableModel = ITGWorkListTableModel.fromJson(body[0][0]);
    return Flexible(child: (itgWorkListTableModel.textvalue).toString().toText12Auto(color: MyColors.normalTextColor));
  } else {
    return Flexible(child: ("-").toString().toText12Auto(color: MyColors.normalTextColor));
  }
}

