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/shared/helpers/base_storage.dart';
import 'package:hub/shared/helpers/storage_helper.dart';

import '/flutter_flow/custom_functions.dart' as functions;
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '../../../shared/services/localization/localization_service.dart';
import 'local_profile_component_model.dart';

export 'local_profile_component_model.dart';

class LocalProfileComponentWidget extends StatefulWidget {
  const LocalProfileComponentWidget({super.key});

  @override
  State<LocalProfileComponentWidget> createState() => _LocalProfileComponentWidgetState();
}

class _LocalProfileComponentWidgetState extends State<LocalProfileComponentWidget> {
  late LocalProfileComponentModel _model;

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

  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => LocalProfileComponentModel());
    _model.setOnUpdate(onUpdate: () => setState(() {}));
    _model.setStateCallback = () => safeSetState(() {});

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      bool initialized = false;
      bool isDevLinked = _model.cliUUID.isNotEmpty;
      if (!isDevLinked && !initialized) {
        initialized = true;
        await LocalizationService.processLocals(context).then((value) => value == true ? onUpdate() : null);
      }
    });
  }

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

    super.dispose();
  }

  void onUpdate() {
    log('() => onUpdate()');
    safeSetState(() {
      _model.getData();
    });
  }

  @override
  Widget build(BuildContext context) {
    final textScaler = MediaQuery.textScalerOf(context);
    final double baseFontSize = 14.0;
    final double scaledFontSize = baseFontSize * textScaler.scale(1);
    final double limitedFontSize = scaledFontSize > 20 ? 12 : scaledFontSize;

    return Container(
      decoration: const BoxDecoration(),
      child: Align(
        alignment: const AlignmentDirectional(0.0, -1.0),
        child: Material(
          color: Colors.transparent,
          elevation: 0.0,
          child: Container(
            width: double.infinity,
            height: 119.0,
            decoration: BoxDecoration(
              color: FlutterFlowTheme.of(context).primary,
              border: Border.all(
                color: FlutterFlowTheme.of(context).primary,
              ),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                Align(
                  alignment: const AlignmentDirectional(-1.0, 0.0),
                  child: Padding(
                    padding: const EdgeInsets.all(2.0),
                    child: InkWell(
                        splashColor: Colors.transparent,
                        focusColor: Colors.transparent,
                        hoverColor: Colors.transparent,
                        highlightColor: Colors.transparent,
                        onTap: () async {
                          await StorageHelper().set(SQLiteStorageKey.clientUUID.value, '', Storage.SQLiteStorage);
                          await LocalizationService.processLocals(context).whenComplete(() => onUpdate());
                        },
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(200.0),
                          child: CachedNetworkImage(
                            imageBuilder: (context, imageProvider) => Container(
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image: imageProvider,
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                            imageUrl: valueOrDefault(
                                'https://freaccess.com.br/freaccess/Images/Clients/${_model.cliUUID}.png',
                                'assets/images/home.png'),
                            width: 80.0,
                            height: 80.0,
                            fit: BoxFit.cover,
                            alignment: const Alignment(0.0, 0.0),
                            placeholder: (context, url) => Image.asset('assets/images/home.png'),
                            errorListener: (_) => Image.asset('assets/images/home.png'),
                            errorWidget: (_, __, ___) => Image.asset('assets/images/home.png'),
                          ),
                        )),
                  ),
                ),
                Expanded(
                  child: Tooltip(
                    message: valueOrDefault<String>(
                      functions.convertToUppercase(_model.cliName),
                      FFLocalizations.of(context).getVariableText(
                        ptText: 'SEM LOCAL VINCULADO',
                        enText: 'NO LINKED LOCAL',
                      ),
                    ),
                    child: Text(
                      valueOrDefault<String>(
                        functions.convertToUppercase(_model.cliName),
                        FFLocalizations.of(context).getVariableText(
                          ptText: 'SEM LOCAL VINCULADO',
                          enText: 'NO LINKED LOCAL',
                        ),
                      ),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: FlutterFlowTheme.of(context).labelMedium.override(
                            fontFamily: 'Nunito',
                            color: FlutterFlowTheme.of(context).info,
                            fontSize: limitedFontSize,
                            letterSpacing: 0.0,
                            fontWeight: FontWeight.w500,
                            useGoogleFonts: GoogleFonts.asMap().containsKey('Nunito'),
                          ),
                    ),
                  ),
                ),
              ]
                  .divide(const SizedBox(width: 20.0))
                  .addToStart(const SizedBox(width: 20.0))
                  .addToEnd(const SizedBox(width: 20.0)),
            ),
          ),
        ),
      ),
    );
  }
}
