import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/qr/qr_share_image.dart';
import 'package:huixiang/retrofit/data/user_info.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';

class QrSharePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _QrSharePage();
  }
}

class _QrSharePage extends State<QrSharePage> {
  String phone = "";

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

    SharedPreferences.getInstance().then((value) {
      if (value.getString('user') != null && value.getString('user') != "") {
        phone = UserInfo.fromJson(jsonDecode(value.getString('user'))).phone;
        setState(() {});
      }
    });

    buildImageInfo();
  }

  @override
  void dispose() {
    SmartDialog.dismiss();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (height == null || height == 0)
      height = MediaQuery.of(context).size.height;
    return Scaffold(
      appBar: MyAppBar(
        title: "",
        titleColor: Colors.black,
        background: Color(0xFFF7F7F7),
        leadingColor: Colors.black,
        action: Container(
          alignment: Alignment.center,
          margin: EdgeInsets.only(right: 16.w),
          child: InkWell(
            onTap: () {
              if (phone == null || phone == "") return;
              SmartDialog.show(
                widget: QrShareImagePage(phone),
                clickBgDismissTemp: true,
                isPenetrateTemp: false,
              );
            },
            child: Icon(
              Icons.share,
              color: Colors.black,
              size: 24.w,
            ),
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          height: height,
          child: Stack(
            children: [
              Positioned(
                child: Image.asset(
                  "assets/image/qr_share_bg.png",
                  fit: BoxFit.fill,
                ),
                top: 0,
                left: 0,
                bottom: 0,
                right: 0,
              ),
              Positioned(
                child: Container(
                  width: MediaQuery.of(context).size.width,
                  height: height * 0.635,
                  padding: EdgeInsets.symmetric(
                    vertical: 28.h,
                    horizontal: 19.w,
                  ),
                  child: Stack(
                    children: [
                      Positioned(
                        child: Image.asset(
                          "assets/image/qr_share_info_bg.png",
                          fit: BoxFit.fill,
                        ),
                        top: 0,
                        left: 0,
                        bottom: 0,
                        right: 0,
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          shareTypeTitle(S.of(context).fangshiyi),
                          Container(
                            margin: EdgeInsets.only(left: 16.w, right: 24.w),
                            child: Text(
                              S.of(context).baocunsaoma,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                color: Color(0xFF2E3552),
                                fontSize: 16.sp,
                                fontWeight: MyFontWeight.regular,
                              ),
                            ),
                          ),
                          Container(
                            width: 152.w,
                            height: 152.w,
                            decoration: BoxDecoration(
                              border: Border.all(
                                color: Color(0xFF2E3552),
                                width: 1,
                              ),
                            ),
                            child: QrImage(
                              data: "http://mp.hx.lotus-wallet.com/pages/invite/index?mobile=${phone ?? ""}",
                              version: QrVersions.auto,
                              size: 200.w,
                              gapless: true,
                            ),
                          ),
                          shareTypeTitle(S.of(context).fangshier),
                          Container(
                            margin: EdgeInsets.only(left: 16.w, right: 24.w),
                            child: Text(
                              S.of(context).fenxiangyaoqing,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                color: Color(0xFF2E3552),
                                fontSize: 16.sp,
                                fontWeight: MyFontWeight.regular,
                              ),
                            ),
                          ),
                          SizedBox(
                            height: 1.h,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                left: 0,
                bottom: 0,
                right: 0,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget shareTypeTitle(typeText) {
    return Container(
      width: 90.w,
      height: 30.h,
      child: Text(
        typeText,
        style: TextStyle(
          fontWeight: MyFontWeight.semi_bold,
          fontSize: 18.sp,
          color: Color(0xFF2E3552),
        ),
      ),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: Color(0xFFFFC93B),
        border: Border.all(
          color: Color(0xFF2E3552),
          width: 1,
        ),
      ),
    );
  }

  Image image;
  double height;

  buildImageInfo() async {
    image = Image.asset("assets/image/qr_share_bg.png");
    image.image
        .resolve(ImageConfiguration())
        .addListener(ImageStreamListener((ImageInfo info, bool _) {
      height = info.image.height.toDouble();
      setState(() {});
    }));
  }
}