
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';

import 'package:twong/api/index.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/models/index.dart';
import 'package:twong/router/index.dart';
import 'package:twong/utils/image_utils.dart';
import 'package:twong/utils/sentry.dart';

class UserPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _UserPageState();
  }
}

class _UserPageState extends State<UserPage> {
  @override
  void initState() {
    super.initState();
    loadData();
  }

  void loadData() async {
    Network.inst.getUserInfo().catchError((err) {
      BotToast.showText(text: err.toString());
    });
  }

  Widget _buildHeader() {
    return Container(
        color: DColors.Main,
        padding: EdgeInsets.only(left: 12.px, right: 12.px),
        child: Cache.user == null ? Column(
          children: <Widget>[
            Container(
              width: 120.px,
              padding: EdgeInsets.only(top: 58.px),
              child: FlatButton(
                  color: Colors.white,
                  child: Text('登录'),
                  shape: StadiumBorder(),
                  onPressed: () {
                    Navigator.pushNamed(context, RouteNames.login);
                  }),
            )
          ],
        ) : Column(
          children: [
            Container(height: 32.px, child: Row(
              children: [
                Spacer(),
                IconButton(icon: Icon(Icons.settings, color: Colors.white),
                    onPressed: () async {
                      Navigator.pushNamed(context, RouteNames.setting);
                    }),
                IconButton(icon: Icon(IconFonts.message, color: Colors.white),
                    onPressed: () {})
              ],
            )),
            Row(
              children: [
                Container(
                    margin: EdgeInsets.only(left: 12.px),
                    child: CircleAvatar(
                      radius: 34.px,
                      backgroundImage: Cache.user?.avatar == null
                          ? null
                          : CachedNetworkImageProvider(Cache.user.avatar),
                    )),
                Expanded(child: Container(
                  margin: EdgeInsets.only(left: 22.px),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(Cache.user.nickname,
                          style: TextStyle(color: Colors.white, fontSize: 16.px)),
                      Text("我的ID: ${Cache.user.uid}",
                          style: TextStyle(color: Colors.white, fontSize: 12.px))
                    ],
                  ),
                )),
              ],
            )
          ],)
    );
  }

  Widget _buildOrder() {
    List<String> titles = ['待付款', '待发货', '待收货', '待评价'];
    List<IconData> icons = [
      Icons.credit_card,
      Icons.shop,
      Icons.assignment_returned,
      Icons.message_outlined
    ];

    List<Widget> widgets = [];
    for (var i = 0; i < titles.length; i ++) {
      widgets.add(Expanded(child: InkWell(
          borderRadius: BorderRadius.circular(45),
          onTap: () {
            Navigator.pushNamed(context, RouteNames.order, arguments: i + 1);
          },
          child: Column(
            children: <Widget>[
              Icon(icons[i]),
              Container(
                padding: EdgeInsets.only(top: 6.px, bottom: 10.px),
                child: Text(titles[i]),
              )
            ],
          )
      )));
    }

    widgets.add(Expanded(child: InkWell(
        borderRadius: BorderRadius.circular(45),
        onTap: () {
          Utils.notOpen();
        },
        child: Column(
          children: <Widget>[
            Icon(Icons.account_box),
            Container(
              padding: EdgeInsets.only(top: 6.px, bottom: 10.px),
              child: Text('客服'),
            )
          ],
        )
    )));

    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(6.px)
      ),
      child: Column(
        children: <Widget>[
          InkWell(
            onTap: () { Navigator.pushNamed(context, RouteNames.order, arguments: 0); },
            child: Container(
                padding: EdgeInsets.only(left: 16.px, right: 12.px, top: 12.px, bottom: 4.px),
                child: Row(children: [
                  Expanded(
                      child: Text("我的订单", style: TextStyle(fontSize: 14.px))),
                  Text("查看所有订单 ", style: TextStyle(color: Colors.grey)),
                  Icon(Icons.keyboard_arrow_right, color: Colors.grey)
                ])),
          ),
          Divider(),
          Row(children: widgets)
        ],
      ),
    );
  }

  Widget _buildMenuList() {
    List<String> titles = ['消息', '收藏', '好友', '帮助', '关于'];
    List<IconData> icons = [
      Icons.message,
      Icons.favorite,
      Icons.people,
      Icons.help,
      Icons.error_outline
    ];
    List<String> routers = [
      RouteNames.message,
      RouteNames.favours,
      RouteNames.friends,
      RouteNames.help,
      RouteNames.about
    ];

    List<Widget> widgets = [];
    for (var idx = 0; idx < titles.length; idx++) {
      widgets.add(InkWell(
        highlightColor: Colors.transparent,
        onTap: () { Navigator.pushNamed(context, routers[idx]); },
        child: Container(
            height: 40.px,
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(6.px)
            ),
            padding: EdgeInsets.only(left: 10.px, right: 10.px),
            margin: EdgeInsets.only(bottom: 8.px, left: 12.px, right: 12.px),
            child: Row(children: [
              Icon(icons[idx]),
              Container(width: 16.px),
              Expanded(child: Text(titles[idx])),
              Icon(Icons.keyboard_arrow_right, color: Colors.grey)
            ])
        ),
      ));
    }
    return Column(children: widgets);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: DColors.Main,
      body: SafeArea(
        child: Container(
          color: DColors.back,
          child: ListView(
            physics: NeverScrollableScrollPhysics(),
            children: <Widget>[
              _buildHeader(),
              Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [DColors.Main, DColors.back],
                    ),
                  ),
                  padding: EdgeInsets.all(8.px),
                  child: _buildOrder()
              ),
              _buildMenuList(),
            ],
          ),
        ),
      ),
    );
  }
}