import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class WinbooksMigrationWidget extends StatefulWidget {
  final String _playStoreUrl;
  final String _appStoreUrl;
  final String _title;

  const WinbooksMigrationWidget(this._title, this._playStoreUrl, this._appStoreUrl, {Key? key}) : super(key: key);

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

class WinbooksMigrationWidgetState extends State<WinbooksMigrationWidget> {
  WinbooksMigrationWidgetState();

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

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    TextStyle largerTextStyle = themeData.textTheme.bodyText1!.apply(fontSizeFactor: 1.4);
    TextStyle smallerTextStyle = themeData.textTheme.bodyText2!;
    AssetBundle assetBundle = DefaultAssetBundle.of(context);
    return Scaffold(
        appBar: AppBar(
          title: Text(widget._title),
        ),
        body: GridView.count(
          crossAxisCount: 1,
          children: [
            Column(children: [
              Container(
                margin: const EdgeInsets.all(10.0),
                child: Text(
                    'My comptaplan migre vers WinBooks Connect. Téléchargez WinBooks Connect pour continuer à envoyer vos fichiers à votre comptable',
                    textAlign: TextAlign.center,
                    style: largerTextStyle),
              ),
              Center(
                child: Container(
                  margin: const EdgeInsets.all(10.0),
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(textStyle: largerTextStyle, padding: const EdgeInsets.all(20.0)),
                    child: Row(children: [
                      Image.asset('images/winbooks_connect.png', bundle: assetBundle, width: 50.0, height: 50.0),
                      const SizedBox(width: 10),
                      const Flexible(child: Text('Télécharger WinBooks Connect', textAlign: TextAlign.left, softWrap: true))
                    ]),
                    onPressed: () async {
                      return _onWinbooksConnectPress();
                    },
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.all(10.0),
                child: Text(
                    'Si vous ne parvenez pas à télécharger Winbooks Connect, ou pour obtenir vos identifiants, veuillez contacter votre comptable.',
                    textAlign: TextAlign.left,
                    style: smallerTextStyle),
              ),
              Center(
                child: Container(
                  margin: const EdgeInsets.all(10.0),
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(textStyle: smallerTextStyle, padding: const EdgeInsets.all(10.0)),
                    child: const Text('Vers My Comptaplan', softWrap: true),
                    onPressed: () async {
                      return _onMyComptaplanPress();
                    },
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.all(10.0),
                child: Text("L'accès à My Comptaplan risque de ne plus fonctionner prochainement.",
                    textAlign: TextAlign.left, style: smallerTextStyle),
              ),
            ])
          ],
        ));
  }

  Future<void> _onWinbooksConnectPress() async {
    if (Platform.isAndroid) {
      AndroidIntent intent = AndroidIntent(
        action: 'action_view',
        data: widget._playStoreUrl,
        package: 'com.android.vending',
      );
      await intent.launch();
    } else if (Platform.isIOS) {
      await launch(widget._appStoreUrl);
    }
  }

  Future<void> _onMyComptaplanPress() async {
    await Navigator.pushNamed(context, '/web');
  }
}
