import 'dart:io';

import 'package:fcs/domain/entities/invoice.dart';
import 'package:fcs/domain/entities/receipt.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/image_file_picker.dart';
import 'package:fcs/pages/widgets/input_text.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/local_title.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:fcs/pages/widgets/show_img.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

class PaymentPageEdit extends StatefulWidget {
  final Receipt receipt;
  PaymentPageEdit({this.receipt});

  @override
  _PaymentPageEditState createState() => _PaymentPageEditState();
}

class _PaymentPageEditState extends State<PaymentPageEdit> {
  TextEditingController _amountController = new TextEditingController();
  var dateFormatter = new DateFormat('dd MMM yyyy');

  Receipt _receipt = new Receipt();
  bool _isLoading = false;
  File _file;

  bool isNew;

  @override
  void initState() {
    if (widget.receipt != null) {
      _receipt = widget.receipt;
    }
    super.initState();
  }

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

  final DateFormat dateFormat = DateFormat("d MMM yyyy");

  @override
  Widget build(BuildContext context) {
    var mainModel = Provider.of<MainModel>(context, listen: false);
    bool customer = mainModel.isCustomer();

    final amountBox = DisplayText(
        labelTextKey: 'pm.amount',
        iconData: FontAwesomeIcons.moneyBill,
        text: _receipt.amount.toString());

    final dateBox = DisplayText(
        labelTextKey: 'pm.date',
        iconData: Icons.date_range,
        text: _receipt.date);

    final statusBox = DisplayText(
        labelTextKey: 'pm.status',
        iconData: Icons.av_timer,
        text: _receipt.status);

    final receiptFileBox = Row(children: [
      LocalText(context, 'pm.attachment', fontSize: 16, color: Colors.grey),
      IconButton(
          icon: Icon(Icons.attachment, color: primaryColor), onPressed: () {})
    ]);

    final comfirmBox =
        fcsButton(context, getLocalString(context, 'pm.btn_confirm'));

    final cancelBox =
        fcsButton(context, getLocalString(context, 'pm.btn_cancel'));

    final fileBox = Container(
      // padding: EdgeInsets.only(top: 20, left: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          LocalText(
            context,
            "pm.attachment",
            fontSize: 16,
            color: labelColor,
          ),
          Container(
            padding: EdgeInsets.only(top: 15),
            child: Stack(
              children: <Widget>[
                Container(
                  width: 200,
                  height: 200,
                  padding: const EdgeInsets.all(2.0),
                  decoration: BoxDecoration(
                    color: Colors.grey[300],
                    border: Border.all(
                      color: primaryColor,
                      width: 1.0,
                    ),
                  ),
                  child: _file == null
                      ? checkImage(context)
                      : enableUpload(context),
                ),
                customer
                    ? Positioned(
                        bottom: -8,
                        right: -10,
                        child: IconButton(
                          color: primaryColor,
                          icon: CircleAvatar(
                            backgroundColor: primaryColor,
                            radius: 20,
                            child: Icon(
                              FontAwesomeIcons.camera,
                              size: 20,
                              color: Colors.white,
                            ),
                          ),
                          onPressed: () =>
                              modelBottomSheet(context, onFile: (file) {
                            setState(() {
                              _file = file;
                            });
                          }),
                        ))
                    : Container()
              ],
            ),
          ),
        ],
      ),
    );

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
          title: Text(AppTranslations.of(context).text("pm_.title")),
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              dateBox,
              SizedBox(height: 10),
              amountBox,
              SizedBox(height: 10),
              statusBox,
              SizedBox(height: 10),
              fileBox,
              SizedBox(height: 5),
              Spacer(),
              customer ? Container() : comfirmBox,
              SizedBox(height: 5),
              customer ? cancelBox : Container(),
              SizedBox(height: 5),
            ],
          ),
        ),
      ),
    );
  }

  Widget checkImage(BuildContext context) {
    if (widget.receipt == null) {
      return initialImage();
    } else {
      Widget _widget;
      if (widget.receipt.fileUrl == null) {
        _widget = initialImage();
      } else {
        _widget = InkWell(
          child: Image.asset(widget.receipt.fileUrl, fit: BoxFit.cover),
          onTap: () {},
        );
      }
      return _widget;
    }
  }

  Widget initialImage() {
    return Center(
      child: Icon(
        Icons.insert_photo,
        size: 45,
        color: labelColor,
      ),
    );
  }

  Widget enableUpload(BuildContext context) {
    return InkWell(
      child: Image.file(_file, fit: BoxFit.cover),
      onTap: () {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    ShowImage(imageFile: _file, url: null, fileName: 'image')));
      },
    );
  }
}
