import 'dart:io';

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/retrofit/data/app_update.dart';
import 'package:huixiang/utils/bridge.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';

import '../generated/l10n.dart';

class UpdateDialog extends StatefulWidget {
  final String version;
  final AppUpdate appUpdate;

  UpdateDialog(this.version, this.appUpdate);

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

class _UpdateDialog extends State<UpdateDialog> {
  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: MediaQuery.of(context).size.width - 80.w,
          decoration: BoxDecoration(
            color: Colors.transparent,
            borderRadius: BorderRadius.circular(8.r),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Stack(
                alignment: Alignment.bottomCenter,
                children: [
                  Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border.all(
                          color: Colors.white,
                          width: 0,
                          style: BorderStyle.solid),
                    ),
                  ),
                  Image.asset(
                    "assets/image/update.webp",
                    width: MediaQuery.of(context).size.width - 80.w,
                    fit: BoxFit.cover,
                  )
                ],
              ),
              Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(
                      color: Colors.white, width: 0, style: BorderStyle.solid),
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(8.r),
                    bottomRight: Radius.circular(8.r),
                  ),
                ),
                padding: EdgeInsets.symmetric(horizontal: 14.w),
                child: Column(
                  children: [
                    SizedBox(
                      height: 32.h,
                    ),
                    Text(
                      widget.appUpdate.appLastVersionExplain ?? "",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 17.sp,
                        height: 1.2,
                        fontWeight: MyFontWeight.regular,
                        color: Color(0xFF333333),
                      ),
                    ),
                    SizedBox(
                      height: 57.h,
                    ),
                    InkWell(
                      onTap: () {
                        updateApp();
                      },
                      child: Container(
                        width: double.infinity,
                        decoration: BoxDecoration(
                          color: Color(0xFF32A060),
                          borderRadius: BorderRadius.circular(27),
                        ),
                        margin: EdgeInsets.only(
                            left: 14.w, right: 14.w, bottom: 8.h),
                        padding: EdgeInsets.symmetric(vertical: 11.h),
                        alignment: Alignment.center,
                        child: Text(
                          S.of(context).lijigengxin,
                          style: TextStyle(
                            fontSize: 17.sp,
                            fontWeight: MyFontWeight.semi_bold,
                            color: Color(0xFFFFFFFF),
                          ),
                        ),
                      ),
                    ),
                    if (!AppUtils.versionCompare(
                        widget.version, widget.appUpdate.appLastVersionUp))
                      InkWell(
                        onTap: () {
                          doNotUpdate();
                        },
                        child: Container(
                          width: double.infinity,
                          alignment: Alignment.center,
                          padding: EdgeInsets.only(top: 8.h, bottom: 15.h),
                          child: Text(
                            S.of(context).yihouzaishuo,
                            style: TextStyle(
                              fontSize: 10.sp,
                              fontWeight: MyFontWeight.regular,
                              color: Color(0xFF8C8C8C),
                            ),
                          ),
                        ),
                      ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  doNotUpdate() async {
    SharedPreferences value = await SharedPreferences.getInstance();
    value.setString("appLastVersion", widget.appUpdate.appLastVersion);
    Navigator.of(context).pop();
  }

  updateApp() async {
    String url = Platform.isIOS
        ? "itms-apps://itunes.apple.com/app/id1575124838"
        : "http://application.lotus-wallet.com/huixiang?release_id=";
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }
}