import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hub/features/home/index.dart';
import 'package:hub/features/menu/index.dart';
import 'package:hub/features/module/data/index.dart';

import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';
import 'package:hub/features/local/index.dart';

class DrawerWidget extends StatelessWidget {
  const DrawerWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<LicenseStatus>(
      stream: LocalsRepositoryImpl.stream,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        } else if (snapshot.hasError) {
          return Center(
            child: Text('Error: ${snapshot.error}'),
          );
        } else if (!snapshot.hasData || snapshot.data! == false) {
          return Center(
            child: Padding(
              padding:
                  EdgeInsets.only(top: MediaQuery.of(context).size.height / 10),
              child: CircularProgressIndicator(
                backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
                color: FlutterFlowTheme.of(context).primary,
              ),
            ),
          );
        }
        return BlocBuilder<HomeBloc, HomeState>(
          builder: (context, state) {
            return SafeArea(
              child: SizedBox(
                width: MediaQuery.of(context).size.width * 0.8,
                child: Drawer(
                  elevation: 16.0,
                  child: Container(
                    color: FlutterFlowTheme.of(context).primaryBackground,
                    child: Column(
                      children: [
                        _buildDrawerHeader(context, state),
                        // _buildSearchBar(context, state),
                        _buildDrawerBody(context),
                      ].addToStart(const SizedBox(height: 20)),
                    ),
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }

  Container _buildDrawerHeader(BuildContext context, HomeState state) {
    return Container(
      decoration: const BoxDecoration(
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(5.0),
          bottomRight: Radius.circular(5.0),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: [
          Flexible(
            fit: FlexFit.loose,
            flex: 1,
            child: Container(
              width: 50.0,
              height: 50.0,
              clipBehavior: Clip.antiAlias,
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
              ),
              child: CachedNetworkImage(
                imageUrl: valueOrDefault(
                  'assets/images/person.jpg',
                  'https://freaccess.com.br/freaccess/Images/Clients/${state.cliUUID}.png',
                ),
                width: 80.0,
                height: 80.0,
                fit: BoxFit.cover,
                alignment: Alignment.center,
                placeholder: (context, url) =>
                    Image.asset('assets/images/person.jpg'),
                errorListener: (_) => Image.asset('assets/images/person.jpg'),
                errorWidget: (_, __, ___) =>
                    Image.asset('assets/images/person.jpg'),
              ),
            ),
          ),
          const SizedBox(width: 10),
          Expanded(
            flex: 1,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  state.userName,
                  style: FlutterFlowTheme.of(context).bodyLarge.override(
                        fontFamily: 'Nunito Sans',
                        color: FlutterFlowTheme.of(context).primaryText,
                        fontSize: 14.0,
                        letterSpacing: 0.0,
                        fontWeight: FontWeight.normal,
                      ),
                ),
                Text(
                  state.userEmail,
                  style: FlutterFlowTheme.of(context).bodySmall.override(
                        fontFamily: 'Nunito Sans',
                        color: FlutterFlowTheme.of(context).primaryText,
                        fontSize: 12.0,
                        letterSpacing: 0.0,
                        fontWeight: FontWeight.normal,
                      ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Padding _buildSearchBar(BuildContext context, HomeState state) {
    final theme = FlutterFlowTheme.of(context);
    final errorColor = theme.error;
    final primaryColor = theme.primary;
    final customColor1 = theme.primaryText;

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10.0),
      child: TextFormField(
        controller:
            TextEditingController(), // Replace with appropriate controller
        focusNode: FocusNode(), // Replace with appropriate focus node
        autofocus: false,
        obscureText: false,
        decoration: InputDecoration(
          isDense: true,
          labelText: 'Search',
          labelStyle: TextStyle(color: customColor1),
          prefixIcon: const Icon(Icons.search_sharp),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(
              color: customColor1,
              width: 0.5,
            ),
            borderRadius: BorderRadius.circular(2.0),
          ),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(
              color: primaryColor,
              width: 0.5,
            ),
            borderRadius: BorderRadius.circular(2.0),
          ),
          errorBorder: UnderlineInputBorder(
            borderSide: BorderSide(
              color: errorColor,
              width: 0.5,
            ),
            borderRadius: BorderRadius.circular(2.0),
          ),
          focusedErrorBorder: UnderlineInputBorder(
            borderSide: BorderSide(
              color: errorColor,
              width: 0.5,
            ),
            borderRadius: BorderRadius.circular(2.0),
          ),
        ),
        style: theme.bodyMedium.override(
          fontFamily: FlutterFlowTheme.of(context).bodyMediumFamily,
          letterSpacing: 0.0,
        ),
        // validator: model.textControllerValidator.asValidator(context),
      ),
    );
  }

  Widget _buildDrawerBody(BuildContext context) {
    return Menufactory(
      entry: MenuEntry.getEntriesByType(MenuEntryType.Drawer),
      item: EnumMenuItem.tile,
      view: MenuView.list,
    );
  }
}
