
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../../generated/l10n.dart';
import '../../routes.dart';
import '../../store/store.dart';
import '../../utils/http_util.dart';
import '../../utils/utils.dart';

class MobileRenewMiniOffice extends StatefulWidget {
  final Map<String, dynamic> data;

  const MobileRenewMiniOffice(this.data, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return MobileRenewMiniOfficeState();
  }

}

class MobileRenewMiniOfficeState extends State<MobileRenewMiniOffice> {

  @override
  Widget build(BuildContext context) {
    Column col = Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [Container(
        padding: EdgeInsets.only(bottom: 16),
        child: Text(
          S.of(context).group_license_renewal,
          style: TextStyle(
            fontSize: 28,
          ),
        ),
      )],
    );
    col.children.add(
      buildLine(S.of(context).group_number, widget.data['group']['name'], valueSize: 18),
    );
    col.children.add(
      buildLine(S.of(context).expiration_date,
          Utils.utcDatetimeStringToLocalDatetimeString(context, widget.data['expiration_date']),
          valueSize: 18
      ),
    );
    col.children.add(
      buildLine(S.of(context).after_renewed,
          Utils.utcDatetimeStringToLocalDatetimeString(context, widget.data['renewed_expiration_date']),
          valueSize: 18
      ),
    );
    col.children.add(
      buildLine(S.of(context).renewal_fee, '\$${widget.data['renewal_fee']}', valueSize: 18),
    );
    col.children.add(
      buildLine(S.of(context).tax, '\$${widget.data['tax']}', valueSize: 18),
    );
    col.children.add(
      buildLine(S.of(context).total, '\$${widget.data['renewal_total']}', valueSize: 38),
    );
    col.children.add(
      Container(
        alignment: Alignment.centerRight,
        padding: EdgeInsets.only(left: 0, right: 0, top: 20, bottom: 20),
        child: ElevatedButton(
          child: Container(
            padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 8),
            child: Text(
              S.of(context).pay_amount_token(widget.data['renewal_total']),
              style: TextStyle(
                fontSize: 20,
              ),
            ),
          ),
          onPressed: () {
            _submit();
          },
        ),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: (){
            Navigator.of(context).pop();
          },
        ),
        title: Text(S.of(context).renew_license),
        backgroundColor: Theme.of(context).primaryColor,
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(top: 20, left: 16, right: 16, bottom: 20),
          child: col,
        ),
      ),
    );
  }

  Widget buildLine(String name, String value, {double nameSize, double valueSize}) {
    Row row = Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [],
    );
    row.children.add(
      Container(
        width: 150,
        padding: EdgeInsets.only(),
        child: Text(
          name,
          style: TextStyle(
            color: Colors.black38,
            fontSize: nameSize,
          ),
        ),
      ),
    );
    row.children.add(
      Expanded(
        child: Align(
          alignment: Alignment.centerRight,
          child: Text(
            value,
            style: TextStyle(
              fontSize: valueSize,
            ),
          ),
        ),
      ),
    );
    return Container(
      padding: EdgeInsets.only(top: 8, bottom: 8),
      child: row,
    );
  }

  void _submit() {
    if (store.state.user == null) {
      Utils.requireLogin(context, returnUrl: '/renew-minioffice/${widget.data['group']['id']}');
      return;
    }
    HttpUtil.httpPost('v1/create-minioffice-renewal-invoice',
          (response) {
        Routes.router.navigateTo(context, '/paynow/${response.data['order_id']}', replace: true);
      },
      body: widget.data,
    ).onError((error, stackTrace) {
      Utils.showMessageDialog(context, error);
    });
  }
}