import 'dart:collection';
import 'dart:developer';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hub/components/templates_components/visit_request_template_component/visit_request_template_component_model.dart';
import 'package:hub/flutter_flow/flutter_flow_icon_button.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';
import 'package:hub/flutter_flow/internationalization.dart';
import 'package:provider/provider.dart';

class VisitRequestTemplateComponentWidget extends StatefulWidget {
  const VisitRequestTemplateComponentWidget({
    Key? key,
    required this.vteName,
    required this.vteReason,
    required this.vteMsg,
    this.vteDocument,
    this.cliUUID,
    this.vteUUID,
    this.vawName,
    this.msgUUID,
    this.vawRef,
    this.vawUUID,
    this.vawDestino,
    required this.vawStatus,
    this.vawDate,
    this.changeStatusAction,
    required this.labelsHashMap,
    required this.statusHashMap,
    required this.imageHashMap,
    this.onTapCardItemAction,
  });

  final Map<String, String> labelsHashMap;
  final List<Map<String, Color>?> statusHashMap;
  final Map<String, String> imageHashMap;
  final Future Function()? onTapCardItemAction;

  final String? vteName;
  final String? vteReason;
  final String? vteMsg;
  final String? vteDocument;
  final String? cliUUID;
  final String? vteUUID;
  final String? vawName;
  final String? msgUUID;
  final String? vawRef;
  final String? vawUUID;
  final String? vawDestino;
  final String? vawStatus;
  final String? vawDate;

  final Future Function(
    BuildContext context,
    String status,
    String vawREF,
    String msg,
    String vteUUID,
  )? changeStatusAction;

  @override
  State<VisitRequestTemplateComponentWidget> createState() =>
      _VisitRequestTemplateComponentWidgetState();
}

class _VisitRequestTemplateComponentWidgetState
    extends State<VisitRequestTemplateComponentWidget> {
  late VisitRequestTemplateComponentModel _model;

  LinkedHashMap<String, String> get labelsLinkedHashMap =>
      LinkedHashMap.from(widget.labelsHashMap ?? {});

  List<LinkedHashMap<String, Color>> get statusLinkedHashMap =>
      widget.statusHashMap
          .map((map) => LinkedHashMap<String, Color>.from(map ?? {}))
          .toList();

  @override
  void setState(VoidCallback callback) {
    super.setState(callback);
    _model.onUpdate();
  }

  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => VisitRequestTemplateComponentModel());

    _model.textController1 ??= TextEditingController(text: widget.vteName);
    _model.textFieldFocusNode1 ??= FocusNode();

    _model.textController2 ??= TextEditingController(text: widget.vteReason);
    _model.textFieldFocusNode2 ??= FocusNode();

    _model.textController3 ??= TextEditingController(text: widget.vteMsg);
    _model.textFieldFocusNode3 ??= FocusNode();

    _model.textController4 ??= TextEditingController(text: widget.vawDate);
    _model.textFieldFocusNode4 ??= FocusNode();

    _model.textController5 ??= TextEditingController();
    _model.textFieldFocusNode5 ??= FocusNode();

    _model.textControllerStatus ??=
        TextEditingController(text: widget.vawStatus);
    _model.textFieldFocusNodeStatus ??= FocusNode();
  }

  @override
  void dispose() {
    _model.maybeDispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    context.watch<FFAppState>();
    bool isLoaded = false;
    log('------------------ VisitRequestTemplateComponentWidget ------------------');
    log('vteName: ${widget.imageHashMap}');
    log('Teste: ${statusLinkedHashMap.expand((linkedHashMap) {
      return [
        linkedHashMap.entries
            .map((MapEntry<String, Color> item) => item.key)
            .first
      ];
    }).first}');
    log('-------------------------------------------------------------------------');
    final pending = FFLocalizations.of(context).getVariableText(
      enText: 'Pending',
      ptText: 'Pendente',
    );
    final active = FFLocalizations.of(context).getVariableText(
      enText: 'Ativo',
      ptText: 'Ativo',
    );
    final canceled = FFLocalizations.of(context).getVariableText(
      enText: 'Canceled',
      ptText: 'Cancelado',
    );

    return LayoutBuilder(
      builder: (context, constraints) {
        return Align(
          alignment: const AlignmentDirectional(0.0, 0.0),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: Container(
              width: constraints.maxWidth,
              decoration: BoxDecoration(
                color: FlutterFlowTheme.of(context).primaryBackground,
                borderRadius: const BorderRadius.all(Radius.circular(25.0)),
              ),
              child: SingleChildScrollView(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    const SizedBox(height: 20.0),
                    Container(
                      width: 150.0,
                      height: 150.0,
                      clipBehavior: Clip.antiAlias,
                      decoration: const BoxDecoration(
                        shape: BoxShape.circle,
                      ),
                      child: CachedNetworkImage(
                        fadeInDuration: const Duration(milliseconds: 100),
                        fadeOutDuration: const Duration(milliseconds: 100),
                        imageUrl:
                            'https://freaccess.com.br/freaccess/getImage.php?cliID=${FFAppState().cliUUID}&atividade=getFoto&Documento=${widget.imageHashMap['key']}&tipo=${widget.imageHashMap['value']}',
                        fit: BoxFit.cover,
                      ),
                    ),
                    const SizedBox(height: 10.0),
                    Row(
                      children: statusLinkedHashMap.expand((linkedHashMap) {
                        return linkedHashMap.entries
                            .map((MapEntry<String, Color> item) {
                          return Expanded(
                            child: Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 24.0),
                              child: TextFormField(
                                // controller: _model.textControllerStatus,
                                // focusNode: _model.textFieldFocusNodeStatus,
                                autofocus: false,
                                canRequestFocus: false,
                                readOnly: true,
                                obscureText: false,
                                decoration: InputDecoration(
                                  isDense: true,
                                  enabledBorder: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(10.0),
                                  ),
                                  filled: true,
                                  fillColor: item.value,
                                  labelText: item.key,
                                  labelStyle: FlutterFlowTheme.of(context)
                                      .labelMedium
                                      .override(
                                        fontFamily: FlutterFlowTheme.of(context)
                                            .labelMediumFamily,
                                        color: FlutterFlowTheme.of(context)
                                            .primaryText,
                                        letterSpacing: 0.0,
                                        useGoogleFonts: GoogleFonts.asMap()
                                            .containsKey(
                                                FlutterFlowTheme.of(context)
                                                    .labelMediumFamily),
                                      ),
                                  hintStyle: FlutterFlowTheme.of(context)
                                      .labelMedium
                                      .override(
                                        fontFamily: FlutterFlowTheme.of(context)
                                            .labelMediumFamily,
                                        color: FlutterFlowTheme.of(context)
                                            .primaryText,
                                        letterSpacing: 0.0,
                                        useGoogleFonts: GoogleFonts.asMap()
                                            .containsKey(
                                                FlutterFlowTheme.of(context)
                                                    .labelMediumFamily),
                                      ),
                                  focusedBorder: InputBorder.none,
                                  errorBorder: InputBorder.none,
                                  focusedErrorBorder: InputBorder.none,
                                  suffixIcon: Icon(
                                    Icons.info,
                                    color: FlutterFlowTheme.of(context).accent1,
                                  ),
                                ),
                                style: FlutterFlowTheme.of(context)
                                    .bodyMedium
                                    .override(
                                      fontFamily: FlutterFlowTheme.of(context)
                                          .bodyMediumFamily,
                                      color: FlutterFlowTheme.of(context)
                                          .primaryText,
                                      letterSpacing: 0.0,
                                      useGoogleFonts: GoogleFonts.asMap()
                                          .containsKey(
                                              FlutterFlowTheme.of(context)
                                                  .bodyMediumFamily),
                                    ),
                                textAlign: TextAlign.start,
                                maxLines: null,
                                keyboardType: TextInputType.name,
                                validator: _model.textController1Validator
                                    .asValidator(context),
                              ),
                            ),
                          );
                        }).toList();
                      }).toList(),
                    ),
                    const SizedBox(height: 10.0),
                    // ListView.builder(
                    //   shrinkWrap: true,
                    //   itemCount: labelsLinkedHashMap.length,
                    //   physics: const NeverScrollableScrollPhysics(),
                    //   itemBuilder: (context, index) {
                    //     String key = labelsLinkedHashMap.keys.elementAt(index);
                    //     String value = labelsLinkedHashMap[key]!;
                    //     return Text('key: $key, value: $value');
                    //   },
                    // ),
                    const SizedBox(height: 20.0),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
