import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:redux/src/store.dart';
import 'package:satu/core/entity/goods_entity.dart';
import 'package:satu/core/models/flow/product_dao.dart';
import 'package:satu/core/redux/actions/sell_actions.dart';
import 'package:satu/core/redux/state/sell_state.dart';
import 'package:satu/core/redux/store.dart';
import 'package:satu/core/services/dictionary_service.dart';
import 'package:satu/core/services/navigator_service.dart';
import 'package:satu/core/utils/locator.dart';
import 'package:satu/routes/route_names.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'package:satu/views/work/tabs/component/product_list_item.dart';
import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/bar/products_header_bar.dart';
import 'package:satu/widgets/bar/products_title_bar.dart';
import 'package:satu/views/work/tabs/utils/ProductUtils.dart';

import 'component/contagent_select_bar.dart';

class SellView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, SellState>(
        converter: (store) => store.state.sellState!,
        builder: (_, state) {
          return Scaffold(
            appBar: ProductsAppBar(
              drawerShow: true,
              title: 'Продажа',
              backgroundColor: backgroundColor,
              childHeight: 60,
              child: ProductHeaderBar(
                count: state.items!.length,
                sum: sumProducts(state.items!),
              ),
            ),
            body: ListView(
              physics: const BouncingScrollPhysics(),
              children: [
                const ContragentSelectBar(
                  value: 'Частное лицо',
                ),
                Visibility(
                  visible: state.items!.isNotEmpty,
                  child: const ProductsTitleBarBar(
                    itemsExist: true,
                    title: 'Товары',
                  ),
                ),
                ListView.separated(
                  physics: const BouncingScrollPhysics(),
                  padding: const EdgeInsets.only(bottom: 120),
                  shrinkWrap: true,
                  itemCount: state.items!.length,
                  itemBuilder: (BuildContext context, int index) {
                    final ProductDao product = state.items!.elementAt(index);
                    return ProductListItem(
                      key: UniqueKey(),
                      ean: product.eanCode,
                      isOdd: index % 2 == 0,
                      name: product.productName,
                      price: product.price,
                      count: product.count,
                      categoryName: product.categoryName,
                      transactionId: product.transactionId,
                    );
                  },
                  separatorBuilder: (context, index) {
                    return const Divider(
                      height: 1,
                      color: disableColor,
                    );
                  },
                ),
              ],
            ),
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerDocked,
            floatingActionButton: floatingActionButtonRender(),
          );
        });
  }

  /// render floating buttons
  Widget floatingActionButtonRender() {
    return StoreConnector<AppState, SellState>(
        converter: (Store<AppState> store) => store.state.sellState!,
        builder: (_, SellState snapshot) {
          return Padding(
            padding: EdgeInsets.all(15.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                Visibility(
                    visible: snapshot.items!.isNotEmpty,
                    child: FloatingActionButton(
                      mini: true,
                      elevation: 2,
                      backgroundColor: successColor,
                      onPressed: () => locator<NavigatorService>()
                          .push(paymentViewRoute),
                      child: Icon(Icons.check, color: whiteColor, size: 35.sp),
                    )),
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    FloatingActionButton(
                      elevation: 2,
                      mini: true,
                      onPressed: () =>
                          locator<NavigatorService>().push(addProductViewRoute),
                      child: Icon(
                        Icons.add_rounded,
                        size: 40.sp,
                        color: whiteColor,
                      ),
                    ),
                    verticalSpaceSmall,
                    FloatingActionButton(
                      elevation: 2,
                      mini: true,
                      onPressed: () async {
                        final NavigatorService _nav =
                            locator<NavigatorService>()
                                    .push(addByBarcodeViewRoute)
                                as NavigatorService;
                        final dynamic result =
                            await _nav.push(addByBarcodeViewRoute);
                        if (result != null) {
                          final List<Good> goods =
                              await locator<DictionaryService>()
                                  .getGoodsByEan(result as String);
                          if (goods.isNotEmpty) {
                            Redux.store
                                ?.dispatch(addSellItem(good: goods.first));
                          }
                        }
                      },
                      child: Icon(Icons.qr_code_rounded,
                          size: 30.sp, color: whiteColor),
                    ),
                  ],
                )
              ],
            ),
          );
        });
  }
}
