
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:twong/api/index.dart';

import 'package:twong/utils/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/models/balance.dart';
import 'package:twong/utils/sentry.dart';
import 'package:twong/widgets/future_widget.dart';

class WalletPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WalletPageState();
  }
}

class _WalletPageState extends State<WalletPage> {
  Balance _balance;

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

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.light,
    ));

    loadData();
    _balance = Cache.get(SaveKey.balance);
  }

  loadData () async {
    Network.inst.getBalance().then((balance) {
      if(!mounted) return;
      setState(() {
        _balance = balance;
      });
      Cache.set(SaveKey.balance, _balance);
    }).catchError((err) {
      Log.e(err);
    });

    SentryUtils.reportError("error", "stackTrace");
  }

  Widget _buildWallet () {
    return Container(
      height: 168.px,
      margin: EdgeInsets.only(right: 16.px, left: 16.px),
      decoration: BoxDecoration(
        color: Colors.red,
        borderRadius: BorderRadius.circular(8.px),
      ),
      child: Container(
        padding: EdgeInsets.all(12.px),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('总资产（元）', style: TextStyle(color: Colors.white, fontSize: 12.px, fontWeight: FontWeight.w100)),
                    RichText(text: TextSpan(
                        text: "￥",
                        style: TextStyle(color: Colors.white, fontSize: 18),
                        children: [ TextSpan(
                            style: TextStyle(color: Colors.white, fontSize: 30),
                            text: _balance?.now_money == null ? '--' : _balance.now_money
                        )]
                    )),
                  ],
                )),
                FlatButton(
                  onPressed: () {},
                  color: Colors.white,
                  shape: RoundedRectangleBorder(side: BorderSide.none, borderRadius: BorderRadius.all(Radius.circular(16.px))),
                  child: Text('充值', style: TextStyle(color: Colors.red)),
                )
              ],
            ),
            Container(
              padding: EdgeInsets.only(top: 40.px),
              child: Row(
                children: <Widget>[
                  Expanded(child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text('累计充值（元）', style: TextStyle(color: Colors.white, fontSize: 12.px, fontWeight: FontWeight.w100)),
                      RichText(text: TextSpan(
                          text: "￥",
                          style: TextStyle(color: Colors.white, fontSize: 12),
                          children: [ TextSpan(
                              style: TextStyle(color: Colors.white, fontSize: 20),
                              text: _balance?.recharge == null ? '--' : _balance.recharge.toString(),
                          )]
                      )),
                    ],
                  )),
                  Expanded(child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text('累计消费（元）', style: TextStyle(color: Colors.white, fontSize: 12.px, fontWeight: FontWeight.w100)),
                      RichText(text: TextSpan(
                          text: "￥",
                          style: TextStyle(color: Colors.white, fontSize: 12),
                          children: [ TextSpan(
                              style: TextStyle(color: Colors.white, fontSize: 20),
                              text: _balance?.orderStatusSum == null ? '--' : _balance.orderStatusSum.toString(),
                          )]
                      )),
                    ],
                  )),
                ],
              ),
            )
          ],
        ),
      )
    );
  }

  Widget _buildMenu () {
    return Container(
      padding: EdgeInsets.only(top: 16.px),
      child: Flex(
        direction: Axis.horizontal,
        children: <Widget>[
          Expanded(child: Container(
            child: GestureDetector(
              onTap: () {},
              child: Column(
                children: <Widget>[
                  Container(height: 6,),
                  Icon(Icons.library_books, color: Colors.red),
                  Text('账单记录', style: TextStyle(fontWeight: FontWeight.w200, fontSize: 12.px)),
                ],
              ),
            ),
          ),),
          Expanded(child: Container(
            child: GestureDetector(
              onTap: () {},
              child: Column(
                children: <Widget>[
                  Container(height: 6.px),
                  Icon(Icons.attach_money, color: Colors.red),
                  Text('消费记录', style: TextStyle(fontWeight: FontWeight.w200, fontSize: 12.px)),
                ],
              ),
            ),
          ),),
          Expanded(child: Container(
            child: GestureDetector(
              onTap: () {},
              child: Column(
                children: <Widget>[
                  Container(height: 6.px),
                  Icon(Icons.monetization_on, color: Colors.red),
                  Text('充值记录', style: TextStyle(fontWeight: FontWeight.w200, fontSize: 12.px)),
                ],
              ),
            ),
          ),),
          Expanded(child: Container(
            child: GestureDetector(
              onTap: () {},
              child: Column(
                children: <Widget>[
                  Container(height: 6.px),
                  Icon(Icons.receipt, color: Colors.red),
                  Text('积分记录', style: TextStyle(fontWeight: FontWeight.w200, fontSize: 12.px)),
                ],
              ),
            ),
          ),)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold (
      backgroundColor: DColors.back,
      body: SafeArea(
        child: ListView(
          physics: ClampingScrollPhysics(),
          children: <Widget>[
            _buildWallet(),
            _buildMenu(),
            Container(
              height: 1.px,
              margin: EdgeInsets.all(10.px),
              color: Color.fromARGB(255, 223, 223, 223),
            )
          ],
        ),
      )
    );
  }
}