import 'dart:convert';

import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/models/ChatMsg.dart';
import 'package:chat/proto/chat.pbserver.dart';
import 'package:chat/r.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:chat/utils/app_navigator.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';

class RedBagItem extends StatefulWidget {
  final MsgModel msgModel;

  final int sendId;

  RedBagItem(Key key, this.sendId, this.msgModel) : super(key: key);

  @override
  _RedBagItemState createState() => _RedBagItemState();
}

class _RedBagItemState extends State<RedBagItem> {
  RedWallet wallet;
  List<int> translateStr;

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

    wallet = RedWallet.fromBuffer(widget.msgModel.msgContent);
    translateStr = widget.msgModel.translateContent;

    MessageMgr().on('Update RedWalletState', updateRedWalletState);
  }

  @override
  void dispose() {
    MessageMgr().off('Update RedWalletState', updateRedWalletState);
    super.dispose();
  }

  updateRedWalletState(walletInfo) {
    if (walletInfo.orderId == wallet.orderId) {
      wallet.state = walletInfo.state;
      if (mounted) {
        setState(() {});
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    var msg;
    var bgColor = Color(0xFFFF9936);
    if (wallet.state == RedWalletState.Expire) {
      msg = I18n.of(context).over_time;
      bgColor = Color(0xFFFF9936).withOpacity(0.5);
    } else if (wallet.state == RedWalletState.Received) {
      msg = I18n.of(context).has_get;
      bgColor = Color(0xFFFF9936).withOpacity(0.5);
    }

    var titleStr;
    if (translateStr != null && translateStr.length > 0) {
      print('红包有翻译内容');
      titleStr = utf8.decode(translateStr);
    } else {
      titleStr = wallet.title;
    }

    print('红包内容：$titleStr');
    var title = SizedBox(
        width: Screen.width - 220,
        child: Text(
          titleStr,
          textScaleFactor: 1.0,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 16, color: Colors.white),
        ));

    var borderRadius;
    double radius = 7.5;
    borderRadius = BorderRadius.all(Radius.circular(radius));

    return GestureDetector(
      child: ClipRRect(
          borderRadius: borderRadius,
          child: Container(
            decoration: BoxDecoration(boxShadow: [
              BoxShadow(
                  color: const Color(0x29B3B4B7),
                  offset: Offset(0, 5),
                  blurRadius: 5.5),
            ]),
            constraints: BoxConstraints(maxWidth: Screen.width - 160),
            child: Column(
              children: <Widget>[
                Container(
                  height: 50,
                  padding: EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                  color: bgColor,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.all(2.5),
                        decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color: Colors.white.withOpacity(0.22)),
                        child: Image.asset(R.assetsImagesCoin, scale: 2),
                      ),
                      SizedBox(width: 5),
                      msg != null
                          ? Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                title,
                                fixedText(msg,
                                    fontSize: 11, color: Colors.white70)
                              ],
                            )
                          : title
                    ],
                  ),
                ),
                Container(
                  height: 20,
                  width: double.infinity,
                  color: Colors.white,
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 20),
                  child: fixedText(I18n.of(context).red_money,
                      fontSize: 10, color: Constants.GreyTextColor),
                )
              ],
            ),
          )),
      onTap: () {
        print('点击红包');
        AppNavigator.pushCoinBagInfoPage(context, widget.msgModel, titleStr);
      },
    );
  }
}
