import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:sport/bean/game.dart';
import 'package:sport/bean/sport_detail.dart';
import 'package:sport/provider/lib/provider_widget.dart';
import 'package:sport/provider/lib/view_state_lifecycle.dart';
import 'package:sport/provider/sport_history_model.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/loading.dart';
import 'package:sport/widgets/misc.dart';
import 'package:sport/widgets/space.dart';

class SportHistoryPage extends StatefulWidget {
  final GameInfoData details;

  SportHistoryPage(this.details);

  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends ViewStateLifecycle<SportHistoryPage, SportHistoryModel> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ProviderWidget<SportHistoryModel>(
          model: model,
          onModelReady: (model) => model.initData(),
          builder: (_, model, __) {
            return EasyRefresh.custom(
              controller: model.refreshController,
              enableControlFinishRefresh: true,
              enableControlFinishLoad: true,
              onRefresh: () => model.refresh(),
              onLoad: model.isIdle ? () => model.loadMore() : null,
              header: buildClassicalHeader(bgColor: Theme.of(context).scaffoldBackgroundColor),
              footer: buildClassicalFooter(),
              slivers: <Widget>[
                SliverAppBar(
                  pinned: true,
                  elevation: 0,
                  expandedHeight: 200,
                  leading: buildBackButton(context),
                  flexibleSpace: FlexibleSpaceBar(
                    background: _buildHeaderWidget(),
                  ),
                ),
                SliverToBoxAdapter(
                  child: SizedBox(
                    height: 50,
                    child: Stack(
                      fit: StackFit.expand,
                      children: <Widget>[
                        Container(
                          width: double.infinity,
                          color: Theme.of(context).scaffoldBackgroundColor,
                          height: 10,
                        ),
                        Container(
                            width: double.infinity,
                            height: 50,
                            padding: EdgeInsets.symmetric(horizontal: 12.0),
                            decoration: BoxDecoration(borderRadius: BorderRadius.vertical(top: Radius.circular(10)), color: Colors.white),
                            child: Align(
                              alignment: Alignment.centerLeft,
                              child: Text(
                                "记录明细",
                                style: Theme.of(context).textTheme.headline3,
                              ),
                            )),
                      ],
                    ),
                  ),
                ),
                if (model.isBusy)
                  SliverToBoxAdapter(
                    child: RequestLoadingWidget(),
                  ),
                if (model.isIdle)
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        if (index.isOdd) {
                          return Divider();
                        }
                        return _buildHistoryItemWidget(model.list[index ~/ 2]);
                      },
                      childCount: model.list.length * 2 - 1,
                    ),
                  ),
                if (model.isEmpty)
                  SliverFillRemaining(
                      hasScrollBody: false,
                      child: Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text("您还未开始任何运动"),
                            Space(
                              height: 10,
                            ),
                            PrimaryButton(
                              content: "前往运动",
                              callback: () {
                                NavigatorUtil.goGameDetails(context, details: widget.details);
                              },
                              width: 134,
                              height: 35,
                            )
                          ],
                        ),
                      ))
              ],
            );
          }),
    );
  }

  Widget _buildHeaderWidget() {
    return Container(
      color: Theme.of(context).scaffoldBackgroundColor,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Container(
              width: 90,
              height: 90,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  image: DecorationImage(
                      fit: BoxFit.cover,
                      image: CachedNetworkImageProvider(widget.details?.cover??'')))),
          Padding(
            padding: const EdgeInsets.fromLTRB(0, 8, 0, 24),
            child: Text(
              "${widget.details?.name}",
              style: Theme.of(context).textTheme.headline3.copyWith(fontWeight: FontWeight.normal),
            ),
          )
        ],
      ),
    );
  }

  Widget _buildHistoryItemWidget(RecordsTodaySum item) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.fromLTRB(12.0, 6, 12.0, 6),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  item.screen == 1 ? "投屏运动" : "手机运动",
                  style: Theme.of(context).textTheme.subtitle1,
                ),
                Space(
                  height: 4,
                ),
                Text(
                  "日期：${item.createdAt}",
                  style: Theme.of(context).textTheme.bodyText1,
                )
              ],
            ),
          ),
          Text(
            "${item.score.toStringAsFixed(1)}",
            style: Theme.of(context).textTheme.subtitle1.copyWith(color: Theme.of(context).accentColor),
          ),
          Text("分", style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 12)),
        ],
      ),
    );
  }

  Widget _buildHistoryDayWidget() {
    return Text("2020.06.21");
  }

  @override
  SportHistoryModel createModel() => SportHistoryModel(widget.details.id);
}
