import 'dart:io';

import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/buttons/custom_text_button.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:in_app_update/in_app_update.dart';
import 'package:url_launcher/url_launcher.dart';

class AppUpdatePage extends StatefulWidget {
  final String appUpdateText;

  AppUpdatePage({required this.appUpdateText});

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

class _AppUpdatePageState extends State<AppUpdatePage> {
  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      appBarTitle: "App Update",
      backgroundColor: Colors.white,
      isShowAppBar: false,
      isShowDecPage: false,
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              Stack(
                children: [
                  Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
                    SvgPicture.asset("assets/images/new-design/update_rocket_image.svg", fit: BoxFit.fill),
                  ]),
                  Container(
                      margin: EdgeInsets.only(top: 40.0),
                      width: MediaQuery.of(context).size.width,
                      child: Text(TranslationBase.of(context).appUpdate,
                          textAlign: TextAlign.center, style: TextStyle(color: Color(0xff2d6c90).withOpacity(1.0), fontSize: 22.0, fontWeight: FontWeight.bold))),
                ],
              ),
              Container(
                margin: EdgeInsets.only(top: 5.0, bottom: 5.0),
                child: SvgPicture.asset("assets/images/new-design/HMG_logo.svg", fit: BoxFit.fill),
              ),
              Container(
                  margin: EdgeInsets.only(top: 10.0, left: 10.0, right: 10.0),
                  width: MediaQuery.of(context).size.width,
                  child: Text(widget.appUpdateText, textAlign: TextAlign.center, style: TextStyle(color: Colors.grey[600], fontSize: 14.0, height: 1.5, fontWeight: FontWeight.w600))),
              Container(
                margin: EdgeInsets.only(left: 20.0, right: 20.0, top: 20.0),
                child: ButtonTheme(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  minWidth: MediaQuery.of(context).size.width,
                  height: 45.0,
                  child: CustomTextButton(
                    backgroundColor: Colors.red[800]!,
                    disabledForegroundColor: Color(0xFFbcc2c4).withOpacity(0.38),
                    disabledBackgroundColor: Color(0xFFbcc2c4).withOpacity(0.12),
                    elevation: 0,
                    onPressed: () {
                      openAppUpdateLink();
                    },
                    child: Text(TranslationBase.of(context).appUpdate, style: TextStyle(fontSize: 18.0, color: Colors.white)),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  openAppUpdateLink() {
    if (Platform.isAndroid) {
      // _launchURL("https://play.google.com/store/apps/details?id=com.ejada.hmg");
      InAppUpdate.checkForUpdate().then((info) {
        print("checkForUpdate!!!");
        print(info.toString());
        if (info.immediateUpdateAllowed) {
          print("Immediate Allowed!!!");
          InAppUpdate.performImmediateUpdate().then((value) {}).catchError((e) => print(e.toString()));
        }
      }).catchError((e) {
        print(e.toString());
        _launchURL("https://play.google.com/store/apps/details?id=com.ejada.hmg");
      });
    }
    if (Platform.isIOS) {
      _launchURL("https://itunes.apple.com/app/id733503978");
    }
  }

  _launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }
}
