import 'package:oc_front/main.dart';
import 'package:flutter/material.dart';
import 'package:oc_front/models/resources/resources.dart';
import 'package:oc_front/models/response.dart';
import 'package:oc_front/pages/shared.dart';
import 'package:oc_front/widgets/inputs/shallow_dropdown_input.dart';
import 'package:oc_front/widgets/inputs/sub_dropdown_input%20.dart';

// ignore: must_be_immutable
class ItemWidget extends StatefulWidget {
  AbstractItem item;
  ItemWidget ({ super.key, required this.item });
  @override ItemWidgetState createState() => ItemWidgetState();
}
class ItemWidgetState extends State<ItemWidget> {
  @override Widget build(BuildContext context) {
    List<Widget> widgets = [
      Container( margin: EdgeInsets.only(bottom: 20),
        decoration: BoxDecoration(border: Border(bottom: BorderSide(color: midColor))),
        width: getMainWidth(context) / 2,
        child: Center(child: Padding( padding: EdgeInsets.only(bottom: 20), 
          child: Text("RESOURCE INFORMATIONS", style: TextStyle(fontSize: 18, color: Colors.grey, fontWeight: FontWeight.w500)))))
    ];
    var infos = widget.item.infos();
    var count = 0;
    for (var info in infos.keys) {
      count++;
      widgets.add(Padding( padding: EdgeInsets.symmetric(vertical: 5), child : Row(children: [
        Padding(  padding: EdgeInsets.only(left: 50, right: 10), child : Text("${info.toUpperCase().replaceAll("_", " ")} :", style: TextStyle(fontSize: 15, color: Colors.grey))),
        Text("${infos[info] is bool ? (infos[info] == true ? "yes" : "no") : infos[info] ?? "unknown"}", 
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.w500))
      ])));
    }
    if (count == 0 ) {
        widgets.add(Center(child: Padding( padding: EdgeInsets.symmetric(vertical: 5), child : Row(children: [
          Padding(  padding: EdgeInsets.only(left: 50, right: 10), child : Text("NO INFORMATION", style: TextStyle(fontSize: 15, color: Colors.grey))),
        ]))));
      }
    List<Widget> widgetsInstance = [];
    List<Shallow> dpItems = [];
    for (var (i, instance) in widget.item.instances.indexed) {
      dpItems.add(Shallow(id: "$i", name: instance.name ?? ""));   
    }
    if (dpItems.isNotEmpty) {
      widgetsInstance.add(Center( child: Padding(padding: EdgeInsets.only(bottom: 15), child: 
        ShallowDropdownInputWidget( all: () async => dpItems, width: (getWidth(context) / 2) - 25, 
        label: "instances", type: CollaborativeAreaType.resource, height: 65,
          current: "${widget.item.selectedInstance}", change: (value) {
            if (value != null) { setState(() { widget.item.selectedInstance = int.parse(value); }); }
          },
        ))
      ));
    }
    if (widget.item.instances.length > widget.item.selectedInstance) {
      var instance = widget.item.instances[widget.item.selectedInstance];
      widgetsInstance.add(Container(height: 20, width: getWidth(context) / 2,));
      var count = 0;
      for (var info in instance.infos().keys) {
        if (instance.infos()[info] == null || instance.infos()[info] is List || instance.infos()[info] is Map) { continue; }
        count++;
        widgetsInstance.add(Center(child: Padding( padding: EdgeInsets.symmetric(vertical: 5), child : Row(children: [
          Padding(  padding: EdgeInsets.only(left: 50, right: 10), child : Text("${info.toUpperCase().replaceAll("_", " ")} :", style: TextStyle(fontSize: 15, color: Colors.grey))),
          Text("${instance.infos()[info] is bool ? (instance.infos()[info] == true ? "yes" : "no") : instance.infos()[info] ?? "unknown"}", 
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.w500))
        ]))));
      }
      if (count == 0 ) {
        widgetsInstance.add(Padding( padding: EdgeInsets.symmetric(vertical: 5), child : Row(children: [
          Padding(  padding: EdgeInsets.only(left: 50, right: 10), child : Text("NO INSTANCE INFORMATION", style: TextStyle(fontSize: 15, color: Colors.grey))),
        ])));
      }
    }
    Widget w = Column( mainAxisSize: MainAxisSize.max, children: widgets );
    Widget w2 = Column( mainAxisSize: MainAxisSize.max, children: widgetsInstance );
    return SizedBox(
            height: getHeight(context) - 200,
            child: SingleChildScrollView(
              child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [
              widget.item.description == null ? Container() : Container(
                width: getMainWidth(context),
                alignment: Alignment.center,
                height: 100,
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border(bottom: BorderSide(color: midColor))),
                padding: const EdgeInsets.symmetric(vertical: 30, horizontal: 50), 
                child: Text(widget.item.description!.length > 350 ? "${widget.item.description!.substring(0, 347)}..." : widget.item.description!,
                style: TextStyle(fontSize: 15, color: Colors.grey, fontWeight: FontWeight.w500))), 
              Row( children: [ 
                Container(padding: const EdgeInsets.symmetric(vertical: 20),
                  height: getHeight(context) - 300,
                  decoration: BoxDecoration(border: Border(right: BorderSide(color: midColor))),
                  alignment: Alignment.topLeft, width: getMainWidth(context) / 2,  child: w),
                Container(height: getHeight(context) - 300,
                  alignment: Alignment.topRight, width: getMainWidth(context) / 2, child: w2)
              ])
          ]
        )
      )
    );
  } 
}