import 'dart:convert';

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/models/itg_forms_models/itg_worklist_table_model.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,
      children: [
        "$title:".toText12(isBold: true, color: const Color(0xff2BB8A6)),
        4.width,
        (value.isEmpty ? "--" : value).toText12(color: MyColors.normalTextColor),
      ],
    );
  }
}

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

  ItemDetailViewGridItem(this.index, this.title, this.value, {Key? key, this.isNeedToShowEmptyDivider = false, this.type = ""}) : 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
          ? Container()
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                title != null ? Flexible(child: "$title:".toText12Auto(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))
                    : Container(),
              ],
            ),
    );
  }
}

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));
  }
}
