import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/ui/app/loading_indicator.dart';
import 'package:invoiceninja_flutter/ui/app/snackbar_row.dart';
import 'package:invoiceninja_flutter/ui/stub/stub_list_item.dart';
import 'package:invoiceninja_flutter/ui/stub/stub_list_vm.dart';
import 'package:invoiceninja_flutter/utils/icons.dart';
import 'package:invoiceninja_flutter/utils/localization.dart';

class StubList extends StatelessWidget {
  const StubList({
    Key key,
    @required this.viewModel,
  }) : super(key: key);

  final StubListVM viewModel;

  @override
  Widget build(BuildContext context) {
    /*
    final localization = AppLocalization.of(context);
    final listState = viewModel.listState;
    final filteredClientId = listState.filterEntityId;
    final filteredClient =
        filteredClientId != null ? viewModel.clientMap[filteredClientId] : null;
    */

    return Column(
      children: <Widget>[
      
              Expanded(
                child: !viewModel.isLoaded
                    ? LoadingIndicator()
                    : RefreshIndicator(
                        onRefresh: () => viewModel.onRefreshed(context),
                        child: viewModel.stubList.isEmpty
                            ? Opacity(
                                opacity: 0.5,
                                child: Center(
                                  child: Text(
                                    AppLocalization.of(context).noRecordsFound,
                                    style: TextStyle(
                                      fontSize: 18.0,
                                    ),
                                  ),
                                ),
                              )
                            : ListView.separated(
                                shrinkWrap: true,
                                separatorBuilder: (context, index) => ListDivider(),
                                itemCount: viewModel.stubList.length,
                                itemBuilder: (BuildContext context, index) {
                                  final stubId = viewModel.stubList[index];
                                  final stub = viewModel.stubMap[stubId];
                                  final user = viewModel.user;

                              void showDialog() => showEntityActionsDialog(
                                  entity: stub,
                                  context: context,
                                  user: user,
                                  onEntityAction: viewModel.onEntityAction);


                                  return StubListItem(
                                     user: viewModel.user,
                                     filter: viewModel.filter,
                                     stub: stub,
                                     onTap: () =>
                                         viewModel.onStubTap(context, stub),
                                     onEntityAction: (EntityAction action) {
                                       if (action == EntityAction.more) {
                                         showDialog();
                                       } else {
                                         viewModel.onEntityAction(
                                             context, stub, action);
                                       }
                                     },
                                     onLongPress: () =>
                                         showDialog(),
                                   );
                                },
                              ),
                      ),
              ),

      
        /*
        filteredClient != null
            ? Material(
                color: Colors.orangeAccent,
                elevation: 6.0,
                child: InkWell(
                  onTap: () => viewModel.onViewEntityFilterPressed(context),
                  child: Row(
                    children: <Widget>[
                      SizedBox(width: 18.0),
                      Expanded(
                        child: Text(
                          '${localization.filteredBy} ${filteredClient.listDisplayName}',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16.0,
                          ),
                        ),
                      ),
                      IconButton(
                        icon: Icon(
                          Icons.close,
                          color: Colors.white,
                        ),
                        onPressed: () => viewModel.onClearEntityFilterPressed(),
                      )
                    ],
                  ),
                ),
              )
            : Container(),
        Expanded(
          child: !viewModel.isLoaded
              ? LoadingIndicator()
              : RefreshIndicator(
                  onRefresh: () => viewModel.onRefreshed(context),
                  child: viewModel.stubList.isEmpty
                      ? Opacity(
                          opacity: 0.5,
                          child: Center(
                            child: Text(
                              AppLocalization.of(context).noRecordsFound,
                              style: TextStyle(
                                fontSize: 18.0,
                              ),
                            ),
                          ),
                        )
                      : ListView.separated(
                          shrinkWrap: true,
                          separatorBuilder: (context, index) => ListDivider(),
                          itemCount: viewModel.stubList.length,
                          itemBuilder: (BuildContext context, index) {
                            final user = viewModel.user;
                            final stubId = viewModel.stubList[index];
                            final stub = viewModel.stubMap[stubId];
                            final client =
                                viewModel.clientMap[stub.clientId] ??
                                    ClientEntity();


                              void showDialog() => showEntityActionsDialog(
                                  entity: stub,
                                  context: context,
                                  user: user,
                                  onEntityAction: viewModel.onEntityAction);



                            return StubListItem(
                                 user: viewModel.user,
                                 filter: viewModel.filter,
                                 stub: stub,
                                 client:
                                     viewModel.clientMap[stub.clientId] ??
                                         ClientEntity(),
                                 onTap: () =>
                                     viewModel.onStubTap(context, stub),
                                 onEntityAction: (EntityAction action) {
                                   if (action == EntityAction.more) {
                                     showDialog();
                                   } else {
                                     viewModel.onEntityAction(
                                         context, stub, action);
                                   }
                                 },
                                 onLongPress: () =>
                                    showDialog(),
                               );
                          },
                        ),
                ),
        ),*/
      ],
    );
  }
}
