import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/data/UserData.dart';
import 'package:chat/data/WebData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/models/ref_name_provider.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';

class MyQr extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 55.5, left: 25, right: 25),
      height: 400,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          _head(context),
          _qrImg(),
          Container(
            padding: EdgeInsets.all(20),
            child: Text(
              I18n.of(context).scan_and_add,
              textScaleFactor: 1.0,
              style: TextStyle(fontSize: 12.5),
            ),
          )
        ],
      ),
    );
  }

  _qrImg() {
    //var headImg = UserData().basicInfo.headimgurl;
    final size = 205.5;

    var dayAfter =
        new DateTime.now().add(new Duration(minutes: 5)).millisecondsSinceEpoch;
    var myId = UserData().basicInfo.userId;
    final groupIdAndTime = 'type=0&sessionId=$myId&expirationTime=$dayAfter';

    var encodeStr = base64Encode(utf8.encode(groupIdAndTime));

    return QrImage(
      data: 'https://henho.jphgames.com/?$encodeStr',
      version: QrVersions.auto,
      size: size,
    );
  }

  _head(BuildContext context) {
    var userInfo = UserData().basicInfo;
    var headImg = userInfo.headimgurl;

    var name = Provider.of<RefNameProvider>(context)
        .getRefName(userInfo.userId, userInfo.nickName);

    var isMan = userInfo.sex == 1;

    var city = userInfo.city;
    var country = userInfo.country;
    return Container(
        padding: EdgeInsets.all(20),
        child: Row(
          children: <Widget>[
            ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: Container(
                    width: 60,
                    height: 60,
                    child: headImg == null || headImg == ''
                        ? Image.asset(Constants.DefaultHeadImgUrl)
                        : CachedNetworkImage(
                            imageUrl: headImg,
                            placeholder: (context, url) =>
                                Image.asset(Constants.DefaultHeadImgUrl)))),
            SizedBox(width: 10),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Container(
                        constraints: BoxConstraints(maxWidth: 170),
                        child: Text(name,
                            textScaleFactor: 1.0,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                                color: Constants.BlackTextColor,
                                fontWeight: FontWeight.w500,
                                fontSize: 16))),
                    SizedBox(width: 5),
                    Icon(
                      IconData(
                        isMan ? 0xe639 : 0xe638,
                        fontFamily: 'iconfont',
                      ),
                      color: isMan
                          ? const Color(0xff0072ff)
                          : const Color(0xffff0486),
                      size: 13,
                    )
                  ],
                ),
                //地址
                SizedBox(height: 10),
                fixedText(
                    country == 'China'
                        ? '${WebData().getCountry(country)}'
                        : '${WebData().getCountry(country)} ${WebData().getCity(city)}',
                    fontSize: 11,
                    color: Constants.GreyTextColor)
              ],
            )
          ],
        ));
  }
}
