
import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/models/product_dao.dart';
import 'package:aman_kassa_flutter/core/route_names.dart';
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
import 'package:aman_kassa_flutter/redux/constants/operation_const.dart';
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
import 'package:aman_kassa_flutter/redux/state/kassa_state.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:aman_kassa_flutter/shared/shared_styles.dart';
import 'package:aman_kassa_flutter/views/home/tabs/kassaView/CatalogBottomSheet.dart';
import 'package:aman_kassa_flutter/views/home/tabs/kassaView/ProductAddBottomSheet.dart';
import 'package:aman_kassa_flutter/views/payment/payment_view.dart';
import 'package:aman_kassa_flutter/widgets/components/ProductListItem.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';

class KassaTab extends StatelessWidget {

  final NavigatorService _navigatorService = locator<NavigatorService>();

  final int index;

  KassaTab(this.index);

  Widget buildItem(BuildContext ctxt, int index, ProductDao productDao) {
    return ProductListItem(
      item: productDao,
      index: index,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(4),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: RaisedButton(
                    padding: EdgeInsets.all(8),
                    color: primaryColor,
                    child: Text(
                      "Добавить",
                      style: buttonBigTitleTextStyle,
                    ),
                    onPressed: () { showModalBottomSheetCatalog(context, 'add');},
                  ),
                ),
              ),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: RaisedButton(
                    padding: EdgeInsets.all(8),
                    color: greenColor,
                    child: Text(
                      "Каталог",
                      style: buttonBigTitleTextStyle,
                    ),
                    onPressed: () { showModalBottomSheetCatalog(context, 'catalog');},
                  ),
                ),
              ),
            ],
          ),
          Expanded(
            child: Container(
              child: StoreConnector<AppState, KassaState>(
                converter: (store) => store.state.kassaState,
                builder: (context, vm) {
                  return ListView.builder(
                      itemCount: vm.kassaItems.length,
                      itemBuilder: (BuildContext ctxt, int index) =>
                          buildItem(ctxt, index, vm.kassaItems[index]));
                }
              ),
            ),
          ),
          Divider(),
          Container(
            margin: const EdgeInsets.symmetric(vertical: 8),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                StoreConnector<AppState, KassaState>(
                  converter: (store) => store.state.kassaState,
                  builder: (context, vm) {
                    return Text(totalCalc(vm.kassaItems), style: TextStyle(fontSize: 25));
                  }
                ),
              ],
            ),
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: RaisedButton(
                    padding: EdgeInsets.all(8),
                    color: redColor,
                    child: Text(
                      "возврат",
                      style: buttonBigTitleTextStyle,
                    ),
                      onPressed: () {
                        _navigatorService.push(PaymentViewRoute, arguments: PaymentModel(mode: SettingModeKassa, operationType: OperationTypeReturn) );
                      }
                  ),
                ),
              ),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: RaisedButton(
                    padding: EdgeInsets.all(8),
                    color: greenColor,
                    child: Text(
                      "оплата",
                      style: buttonBigTitleTextStyle,
                    ),
                    onPressed: () {
                      _navigatorService.push(PaymentViewRoute, arguments: PaymentModel(mode: SettingModeKassa, operationType: OperationTypePay) );
                    },
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  String totalCalc(List<ProductDao> kassaItems) {
    num total = 0.0;
    kassaItems.forEach((element) {
      total+= element.total == null ? 0.0 : element.total.toDouble();
    });
    return total.toString();
  }

  void showModalBottomSheetCatalog(BuildContext context, String action) {
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (context){
        return DraggableScrollableSheet(
          initialChildSize: 0.8,
          maxChildSize: 0.95,
          minChildSize: 0.5,
          builder: (BuildContext context, ScrollController scrollController) {
            if( action == 'add') {
              return ProductAddBottomSheet(scrollController: scrollController,);
            }  else {
              return CatalogBottomSheet(scrollController: scrollController,);
            }
          },
        );
      }
    );
  }


}
