import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:satu/core/redux/state/sell_state.dart';
import 'package:satu/core/redux/store.dart';
import 'package:satu/core/utils/utils_parse.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/views/work/views/payment/component/combine_dock.dart';
import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/bar/products_header_bar.dart';
import 'package:satu/views/work/tabs/utils/product_utils.dart';
import 'package:satu/widgets/buttons/busy_button.dart';
import 'package:satu/widgets/fields/input_field.dart';
import 'package:satu/widgets/fields/line_checkbox.dart';

class PaymentView extends StatefulWidget {
  @override
  _PaymentViewState createState() => _PaymentViewState();
}

class _PaymentViewState extends State<PaymentView> {
  bool combine = true;
  late num _sum;

  late TextEditingController _bankSumCtrl;
  late TextEditingController _cashSumCtrl;

  @override
  void initState() {
    super.initState();
    _sum = sumProducts(Redux.store!.state.sellState!.items!);
    _bankSumCtrl = TextEditingController(text: formatDecimal(_sum.toDouble()));
    _cashSumCtrl = TextEditingController(text: '0');
  }

  @override
  void dispose() {
    _bankSumCtrl.dispose();
    _cashSumCtrl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, SellState>(
        converter: (store) => store.state.sellState!,
        builder: (_, state) {
          return Scaffold(
            appBar: ProductsAppBar(
              title: 'Способ оплаты',
              backgroundColor: backgroundColor,
              childHeight: 60,
              child: ProductHeaderBar(
                count: state.items!.length,
                sum: _sum,
              ),
            ),
            body: ListView(
              physics: const BouncingScrollPhysics(),
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Visibility(
                        visible: combine == false,
                        child: const Text(
                          'Способ оплаты',
                          style:
                              TextStyle(fontSize: 14, color: placeholderColor),
                        ),
                      ),
                      Row(
                        children: [
                          const Text(
                            'комбинированный',
                            style: TextStyle(
                                fontSize: 12, color: placeholderColor),
                          ),
                          Switch(
                            value: combine,
                            onChanged: (vl) {
                              setState(() {
                                combine = !combine;
                              });
                            },
                          ),
                        ],
                      )
                    ],
                  ),
                ),
                buildPaymentSelect(),
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 45, vertical: 30),
                  child: BusyButton(title: 'ОПЛАТА', onPressed: () {}),
                ),
              ],
            ),
          );
        });
  }

  Column buildPaymentSelect() {
    if (combine) {
      return Column(
        children: [
          InputField(
            controller: _bankSumCtrl,
            labelText: 'Банковская карта',
            placeholder: 'Укажите сумму',
            suffixText: '₸',
            textInputType: const TextInputType.numberWithOptions(decimal: true),
          ),
          InputField(
            controller: _cashSumCtrl,
            labelText: 'Наличные',
            placeholder: 'Укажите сумму наличных',
            suffixText: '₸',
            textInputType: const TextInputType.numberWithOptions(decimal: true),
          ),
          const CombineDock()
        ],
      );
    }
    return Column(
      children: const [
        LineCheckBox(
          'Банковская карта',
          value: true,
        ),
        Divider(
          height: 1,
          color: disableColor,
        ),
        LineCheckBox(
          'Оплата наличными',
          value: false,
        ),
      ],
    );
  }
}
