import 'package:chat/data/constants.dart';
import 'package:chat/models/gift_item_model.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:chat/utils/TokenMgr.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:chat/generated/i18n.dart';
import 'package:oktoast/oktoast.dart';

class GoddessHotPage extends StatefulWidget {
  final int userId;

  GoddessHotPage({Key key, this.userId}) : super(key: key);

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

class GoddessHotPageState extends State<GoddessHotPage> {
  List<GiftItemModel> giftList = [];

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

  Future getGiftInfo() async {
    Map data = {
      "userId": widget.userId,
    };
    data['sign'] = TokenMgr().getSign(data);

    Response res = await HttpUtil().post('reward/user/stage',
        data: data, failback: () => Navigator.of(context).pop());
    var resData = res.data;
    print(resData);
    if (resData['code'] == 0) {
      giftList = resData['data']
          .map<GiftItemModel>((json) => GiftItemModel.havedFromJson(json))
          .toList();
      setState(() {});
    } else {
      showToast(resData['msg']);
    }
  }

  Widget _buildTips() {
    return Container(
      margin: EdgeInsets.only(left: 12, top: 15),
      child: Text(I18n.of(context).Goddess_heat_tips,
          textScaleFactor: 1.0,
          style: TextStyle(fontSize: 13, color: Constants.BlackTextColor)),
    );
  }

  Widget _buildGift(int id, String name, int nums) {
    return Container(
      child: Column(
        children: <Widget>[
          Stack(
            children: <Widget>[
              Container(
                width: 63,
                height: 71,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(6),
                    border: Border.all(
                        color: nums == 0
                            ? Colors.transparent
                            : const Color(0xFFFD5E83))),
                alignment: Alignment.center,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                        child: Image.asset(
                      "assets/images/gift_$id.png",
                      width: 30,
                    )),
                    Text(
                      name,
                       textScaleFactor: 1.0,
                      style: TextStyle(
                          fontSize: 10, color: const Color(0xFF777777)),
                    ),
                    SizedBox(height: 9),
                  ],
                ),
              ),
              Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6),
                  color: nums == 0
                      ? Colors.black.withOpacity(0.45)
                      : Colors.transparent,
                ),
                width: 63,
                height: 71,
              )
            ],
          ),
          SizedBox(height: 9),
          Text(
            nums.toString() + I18n.of(context).one,
             textScaleFactor: 1.0,
            style: TextStyle(fontSize: 12.5, color: const Color(0xFF515151)),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      elevation: 1,
      title: new Text(
        I18n.of(context).Goddess_heat,
        textScaleFactor: 1.0,
      ),
      leading: CustomUI.buildCustomLeading(context),
      centerTitle: true,
    );

    return Scaffold(
      appBar: appBar,
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            _buildTips(),
            SizedBox(height: 22.5),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: giftList
                  .map((gift) => _buildGift(gift.id, gift.name, gift.amount))
                  .toList(),
            )
          ],
        ),
      ),
    );
  }
}
