import 'dart:developer';

import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hub/flutter_flow/flutter_flow_icon_button.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';
import 'package:hub/flutter_flow/internationalization.dart';
import 'package:hub/shared/utils/log_util.dart';
import 'package:stack_trace/stack_trace.dart';

import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';

class AboutSystemPage extends StatefulWidget {
  @override
  _AboutSystemPageState createState() => _AboutSystemPageState();
}

class _AboutSystemPageState extends State<AboutSystemPage> {
  String _appVersion = '';

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

  Future<void> _loadAppVersion() async {
    try {
      final PackageInfo packageInfo = await PackageInfo.fromPlatform();
      setState(() {
        _appVersion = FFLocalizations.of(context).getVariableText(
          ptText: 'Versão do Aplicativo: ${packageInfo.version}',
          enText: 'App Version: ${packageInfo.version}',
        );
      });
    } catch (e, s) {
      log(e.toString(), stackTrace: s);
      setState(() {
        _appVersion = FFLocalizations.of(context).getVariableText(
          ptText: 'Versão do Aplicativo Indisponível',
          enText: 'App Version Unavailable',
        );
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = FlutterFlowTheme.of(context);
    return Scaffold(
      backgroundColor: theme.primaryBackground,
      appBar: _buildAppBar(context, theme),
      body: _buildBody(),
    );
  }

  PreferredSizeWidget _buildAppBar(
      BuildContext context, FlutterFlowTheme theme) {
    return AppBar(
      backgroundColor: theme.primaryBackground,
      automaticallyImplyLeading: false,
      leading: _buildBackButton(context, theme),
      title: _buildTitle(context, theme),
      centerTitle: true,
      elevation: 0.0,
    );
  }

  Widget _buildBackButton(BuildContext context, FlutterFlowTheme theme) {
    return FlutterFlowIconButton(
      borderColor: Colors.transparent,
      borderRadius: 30.0,
      borderWidth: 1.0,
      buttonSize: 60.0,
      icon: Icon(
        Icons.keyboard_arrow_left,
        color: theme.primaryText,
        size: 30.0,
      ),
      onPressed: () => Navigator.of(context).pop(),
    );
  }

  Widget _buildTitle(BuildContext context, FlutterFlowTheme theme) {
    return Text(
      FFLocalizations.of(context).getVariableText(
        ptText: 'Sobre o Sistema',
        enText: 'About the System',
      ),
      style: FlutterFlowTheme.of(context).headlineMedium.override(
            fontFamily: FlutterFlowTheme.of(context).headlineMediumFamily,
            color: FlutterFlowTheme.of(context).primaryText,
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            letterSpacing: 0.0,
            useGoogleFonts: GoogleFonts.asMap()
                .containsKey(FlutterFlowTheme.of(context).headlineMediumFamily),
          ),
    );
  }

  Future<void> _launchURL() async {
    final Uri url = Uri.parse('https://fre.com.br/');
    if (!await launchUrl(url)) {
      throw Exception('Could not launch $url');
    }
  }

  Widget _buildBody() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 20),
          _buildLogo(),
          SizedBox(height: 20),
          _buildAppVersionText(),
          Spacer(),
          _buildLaunch(),
          SizedBox(height: 20),
        ],
      ),
    );
  }

  Widget _buildLogo() {
    return SizedBox(
      height: 100,
      width: 100,
      child: Image.asset('assets/images/fre.png'),
    );
    // return GestureDetector(
    //   onTap: () {
    //     final exception = Exception('Crashando');
    //     final stackTrace = Trace.current();
    //     LogUtil.requestAPIFailed("proccessRequest.php", "", "Consulta de Pets",
    //         exception, stackTrace);
    //     FirebaseCrashlytics.instance.crash();
    //   },
    //   child: SizedBox(
    //     height: 100,
    //     width: 100,
    //     child: Image.asset('assets/images/fre.png'),
    //   ),
    // );
  }

  Widget _buildLaunch() {
    final theme = FlutterFlowTheme.of(context).primary;
    return RichText(
      text: TextSpan(
        text: 'Website',
        style: TextStyle(color: theme, fontSize: 16),
        recognizer: TapGestureRecognizer()..onTap = () => _launchURL(),
      ),
    );
  }

  Widget _buildAppVersionText() {
    final theme = FlutterFlowTheme.of(context);
    return Text(
      _appVersion,
      style: theme.headlineMedium.override(
        fontFamily: theme.headlineMediumFamily,
        color: theme.primaryText,
        fontSize: 16.0,
        letterSpacing: 0.0,
        useGoogleFonts:
            GoogleFonts.asMap().containsKey(theme.headlineMediumFamily),
      ),
    );
  }
}
