import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:hub/backend/schema/enums/enums.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';
import 'package:hub/pages/home_page/home_page_model.dart';

import '../../../components/organism_components/menu_component/menu_component_widget.dart';
import '../../../flutter_flow/flutter_flow_theme.dart';

class CustomDrawer extends StatelessWidget {
  const CustomDrawer({super.key, required this.model});
  final HomePageModel model;

  @override
  Widget build(BuildContext context) {
    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),
                // _buildSearchBar(context),
                _buildDrawerBody(),
              ].addToStart(const SizedBox(height: 20)),
            ),
          ),
        ),
      ),
    );
  }

  Container _buildDrawerHeader(BuildContext context) {
    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/${model.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(
                  model.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(
                  model.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) {
    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: model.textController,
        focusNode: model.textFieldFocusNode,
        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() {
    return wrapWithModel(
      model: model.menuComponentModel,
      updateCallback: () {},
      child: const MenuComponentWidget(
        expandable: false,
        style: MenuView.list,
        item: MenuItem.tile,
      ),
    );
  }
}
