import 'package:fcs/domain/entities/carton_size.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'carton_size_editor.dart';
import 'model/carton_size_model.dart';

typedef void FindCallBack();

class CartonSizeList extends StatefulWidget {
  const CartonSizeList();
  @override
  _CartonSizeListState createState() => _CartonSizeListState();
}

class _CartonSizeListState extends State<CartonSizeList> {
  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
  }

  List<Widget> showCartonSize(
      BuildContext context, List<CartonSize> cartonSizes) {
    return cartonSizes.map((p) {
      return new ListTile(
          onTap: () {
            Navigator.of(context).push<void>(CupertinoPageRoute(
                builder: (context) => CartonSizeEditor(cartonSize: p)));
          },
          title: new Row(
            children: <Widget>[
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    new Text(
                      p.name ?? "",
                      style: TextStyle(fontSize: 15.0),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 8.0),
                      child: new Text(
                        "(L${p.length}xW${p.width}xH${p.height})",
                        style: new TextStyle(color: Colors.grey),
                      ),
                    ),
                    SizedBox(height: 15)
                  ],
                ),
              ),
              //Spacer(),
              IconButton(
                icon: Icon(Icons.remove, color: primaryColor),
                onPressed: () => _remove(p),
              )
            ],
          ));
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    List<CartonSize> cartonSizes =
        Provider.of<CartonSizeModel>(context).cartonSizes;

    return LocalProgress(
        inAsyncCall: _isLoading,
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon:
                  new Icon(CupertinoIcons.back, color: primaryColor, size: 30),
              onPressed: () => Navigator.of(context).pop(),
            ),
            shadowColor: Colors.transparent,
            backgroundColor: Colors.white,
            title: LocalText(
              context,
              "box.carton_size.title",
              fontSize: 20,
              color: primaryColor,
            ),
          ),
          body: Padding(
            padding: const EdgeInsets.only(left: 12.0, right: 12),
            child: ListView(
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(right: 18.0),
                      child: IconButton(
                        icon: Icon(Icons.add, color: primaryColor),
                        onPressed: () {
                          Navigator.of(context).push<void>(CupertinoPageRoute(
                              builder: (context) => CartonSizeEditor()));
                        },
                      ),
                    ),
                  ],
                ),
                Column(
                  children: showCartonSize(context, cartonSizes),
                ),
                SizedBox(
                  height: 10,
                )
              ],
            ),
          ),
        ));
  }

  _remove(CartonSize cartonSize) {
    if (cartonSize == null) {
      showMsgDialog(context, "Esrror", "Invalid cartonsize!");
      return;
    }
    showConfirmDialog(context, "box.carton_size_remove.confirm",
        () => _removeCartonSize(cartonSize));
  }

  _removeCartonSize(CartonSize cartonSize) async {
    setState(() {
      _isLoading = true;
    });
    CartonSizeModel cartonSizeModel =
        Provider.of<CartonSizeModel>(context, listen: false);
    try {
      await cartonSizeModel.deleteCartonSize(cartonSize.id!);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
