//import 'package:flutter/.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/rank_info.dart';
import 'package:sport/constant/ui.dart' show ui_padding, ui_margin_list;
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/widgets/label.dart';
import 'package:sport/widgets/loading.dart';
import 'package:sport/widgets/misc.dart';

import 'game_info.dart';

class RankInfo extends StatefulWidget {
  @override
  _RankInfoState createState() {
    // TODO: implement createState
    return _RankInfoState();
  }
}

class _RankInfoState extends State<RankInfo> with InjectApi, AutomaticKeepAliveClientMixin {
  List<RankInfoData> _rankData = [];

  @override
  void initState() {
    super.initState();
    // 做一手简单的缓存 全局变量好像不太好...
    if (_rankData == null || _rankData.isEmpty) {
      api.getRankInfoAll().then((data) {
        setState(() {
          _rankData = data.results ?? [];
        });
      });
    }
  }

  List<Widget> getRankHeaderItem() {
    int num = 0;
    List<Widget> _rankHeaders = [];
    for (var item in _rankData) {
      num++;
      if (item.isGame == 0) {
        _rankHeaders.add(RankHeaderItem(item.name, item.introduce, "${item.rateBegin} ~ ${item.rateEnd}", num, item.id));
      }
    }
    return _rankHeaders;
  }

  List<Widget> getRankGameInfo() {
    List<Widget> _rankGames = [];
    for (var item in _rankData) {
      if (item.isGame == 1) {
        _rankGames.add(GameItem(type: 2, name: item.name, imgUrl: item.game.cover, desc: item.introduce, time: "${item.rateBegin} ~ ${item.rateEnd}", id: item.gameId));
      }
    }
    return _rankGames;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (_rankData != null && _rankData.isNotEmpty) {
      return SingleChildScrollView(
        padding: EdgeInsets.zero,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            // header
            Column(
              children: getRankHeaderItem(),
            ),
            // label
            Padding(
              padding: const EdgeInsets.only(top: 5.0),
              child: buildLabelWidget(context, "运动评分榜"),
            ),
            // 游戏评分榜
            Column(
              children: getRankGameInfo(),
            ),
            SizedBox(
              height: 20,
            )
          ],
        ),
      );
    } else {
      return RequestLoadingWidget();
    }
  }

  @override
  bool get wantKeepAlive => true;
}

// header
class RankHeaderItem extends StatelessWidget {
  String _title;
  String _intro;
  dynamic _time;
  int _index;
  int _id;

  RankHeaderItem(this._title, this._intro, this._time, this._index, this._id);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () =>
          NavigatorUtil.goRankDetails(context, _id, 0),
      child: Container(
        width: double.infinity,
        margin: EdgeInsets.fromLTRB(ui_padding, 6.0, ui_padding, 6.0),
        padding: EdgeInsets.all(ui_padding),
        decoration: new BoxDecoration(
          borderRadius: new BorderRadius.all(Radius.circular(10.0)), // 也可控件一边圆角大小
          image: DecorationImage(
            image: AssetImage("lib/assets/img/rank_bg$_index.png"), // NetWorkImage 返回的是Image
            fit: BoxFit.cover,
            //设置四周边框
          ),
        ),
        child: Padding(padding: EdgeInsets.symmetric(vertical: 5.0), child: RankHeaderIntro(title: "$_title", intro: "$_intro", time: "$_time")),
      ),
    );
  }
}

// header 里面的封装
class RankHeaderIntro extends StatelessWidget {
  String title;
  String intro;
  String time;

  RankHeaderIntro({@required this.title, @required this.intro, @required this.time});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
          title,
          style: Theme.of(context).textTheme.headline3.copyWith(color: Colors.white),
        ),
        Padding(
          child: Text(intro,
              style: Theme.of(context).textTheme.bodyText1),
          padding: EdgeInsets.symmetric(vertical: 4.0),
        ),
        Text(time, style: Theme.of(context).textTheme.bodyText1)
      ],
    );
  }
}

// 游戏评分榜
//class RankGameInfo extends StatelessWidget {
//  @override
//  Widget build(BuildContext context) {
//    // TODO: implement build
//    return Container(
//      padding: EdgeInsets.only(bottom: 30),
//      child: Column(
//        children: <Widget>[
//          ListView.builder(
//              shrinkWrap: true,
//              itemBuilder: (context, index) {
//                return GameItem(type: 2);
//              },
//              itemCount: 3)
//        ],
//      ),
//    );
//  }
//}
