import 'dart:convert';

import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/models/group_info_model.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/image_util.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:chat/models/ref_name_provider.dart';
import 'package:provider/provider.dart';

class GroupQrPage extends StatelessWidget {
  final GroupInfoModel groupInfoModel;

  GroupQrPage(this.groupInfoModel);

  @override
  Widget build(BuildContext context) {
    print('groupInfoModel.askSwitch ${groupInfoModel.askSwitch}');
    DateTime today = DateTime.now();
    DateTime fiftyDaysFromNow = today.add(new Duration(days: 7));
    String dayTips = I18n.of(context).group_qr_code_tips;
    dayTips = dayTips.replaceAll('/s1', fiftyDaysFromNow.month.toString());
    dayTips = dayTips.replaceAll('/s2', fiftyDaysFromNow.day.toString());

    return Scaffold(
        appBar: AppBar(
          backgroundColor: AppColors.NewAppbarBgColor,
          title: new Text(
            I18n.of(context).group_qr_code,
            style: TextStyle(color: AppColors.NewAppbarTextColor),
            textScaleFactor: 1.0,
          ),
          centerTitle: true,
          leading: CustomUI.buildCustomLeading(context),
        ),
        body: Center(
            child: Container(
          margin: EdgeInsets.fromLTRB(28, 0, 28, 0),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(6),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              _head(context),
              _qrImg(context),
              Container(
                padding: EdgeInsets.all(20),
                child: Text(
                  dayTips,
                  textScaleFactor: 1.0,
                  style: TextStyle(fontSize: 9),
                ),
              )
            ],
          ),
        )));
  }

  _qrImg(context) {
    //var headImg = UserData().basicInfo.headimgurl;
    final size = Screen.width * 0.7;
    var dayAfter =
        new DateTime.now().add(new Duration(days: 7)).millisecondsSinceEpoch;
    print('groupInfoModel.sessionId ${groupInfoModel.sessionId}');
    final groupIdAndTime =
        'type=1&sessionId=${groupInfoModel.sessionId}&expirationTime=$dayAfter';

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

    return Stack(
      children: <Widget>[
        QrImage(
          data: 'https://henho.jphgames.com/?$encodeStr',
          version: QrVersions.auto,
          size: size,
        ),
        groupInfoModel.askSwitch == 0
            ? Positioned.fill(child: Container())
            : Positioned.fill(
                child: Container(
                //      height: size,
                padding: EdgeInsets.only(left: 20, right: 20),
                alignment: Alignment.center,
                color: Color(0xf7FFFFFF),
                child: Text(
                  I18n.of(context).group_open_verification,
                  textScaleFactor: 1.0,
                  style: TextStyle(color: Color(0xff696969), fontSize: 20),
                ),
              ))
      ],
    );
  }

  _head(BuildContext context) {
    Map refMap = Provider.of<RefNameProvider>(context).refMap;
    var name = groupInfoModel.getGroupName(refMap);

    return Container(
        padding: EdgeInsets.all(20),
        child: Row(
          children: <Widget>[
            ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: WidgetUtil.getAvatarNew(groupInfoModel, key, (path) {
                  groupInfoModel.image = path;
                })),
            SizedBox(width: 10),
            Expanded(
                child: Text(name,
                    textScaleFactor: 1.0,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        color: Constants.BlackTextColor,
                        fontWeight: FontWeight.w500,
                        fontSize: 16)))
          ],
        ));
  }
}
