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:satu/core/entity/Goods.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/views/work/tabs/component/products_app_bar.dart';
import 'package:satu/views/work/tabs/component/products_header_bar.dart';
import 'package:satu/views/work/tabs/component/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: 'Продажа',
              child: ProductHeaderBar(
                count: state.items!.length,
                sum: sumProducts(state.items!),
              ),
              backgroundColor: backgroundColor,
              childHeight: 60,
            ),
            body: Column(
              children: [
                ContragentSelectBar(
                  value: 'Частное лицо',
                ),
                Visibility(child: ProductsTitleBarBar(itemsExist: true), visible: state.items!.isNotEmpty,),
                ListView.separated(
                  shrinkWrap: true,
                  physics: BouncingScrollPhysics(),
                  itemCount: state.items!.length,
                  itemBuilder: (BuildContext context, int index) {
                    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 Divider(height: 1, color: disableColor,);
                  },
                ),
              ],
            ),
            floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
            floatingActionButton: floatingActionButtonRender(),
          );
        });
  }

  Widget floatingActionButtonRender() {
    return StoreConnector<AppState, SellState>(
        converter: (store) => store.state.sellState!,
        builder: (_, 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(
                      elevation: 2,
                      backgroundColor: successColor,
                      onPressed: () => print('check'),
                      child: Icon(Icons.check, color: whiteColor, size: 35.sp),
                    )),
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    FloatingActionButton(
                      elevation: 2,
                      onPressed: () => locator<NavigatorService>().push(AddProductViewRoute),
                      child: Icon(
                        Icons.add_rounded,
                        size: 45.sp,
                        color: whiteColor,
                      ),
                    ),
                    verticalSpaceMedium,
                    FloatingActionButton(
                      elevation: 2,
                      onPressed: () async {
                        String? result = await locator<NavigatorService>().push(AddByBarcodeViewRoute);
                        if(result !=null) {
                          List<Good> goods = await locator<DictionaryService>().getGoodsByEan(result);
                          if(goods.isNotEmpty) {
                            Redux.store?.dispatch(addSellItem( good: goods.first));
                          }
                        }
                      },
                      child: Icon(Icons.qr_code_rounded, size: 35.sp, color: whiteColor),
                    ),
                  ],
                )
              ],
            ),
          );
        });
  }
}
