import 'package:modelo/charts/radial_gauge.dart';
import 'package:modelo/funcoes_gerais.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:line_icons/line_icons.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen>
    with AutomaticKeepAliveClientMixin<HomeScreen> {
  bool error = false;
  String msg = '';
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(FuncoesGerais.nomeApp),
        centerTitle: false,
        actions: [
          IconButton(
              onPressed: () {
                if (Get.theme.brightness == Brightness.dark) {
                  Get.changeThemeMode(ThemeMode.light);
                } else {
                  Get.changeThemeMode(ThemeMode.dark);
                }
              },
              tooltip: 'Alternar modo dark/light',
              icon: Icon(LineIcons.palette)),
          // IconButton(
          //     onPressed: () {
          //       Get.bottomSheet(BottomSheet(
          //           onClosing: () => null,
          //           builder: (_) {
          //             return Padding(
          //               padding: const EdgeInsets.all(8.0),
          //               child: RadialEaseExample(),
          //             );
          //           }));
          //       //Get.to(() => Scaffold(appBar: AppBar(),body: RadialEaseExample()));
          //     },
          //     icon: Icon(Icons.monetization_on_outlined))
        ],
      ),
      body: SafeArea(
        child: this.error
            ? this._buildError()
            : WebView(
                initialUrl: FuncoesGerais.homeURL,
                javascriptMode: JavascriptMode.unrestricted,
                onWebResourceError: (err) {
                  this.error = true;
                  this.msg = err.description;
                  setState(() {});
                },
                allowsInlineMediaPlayback: true,
                navigationDelegate: (navigation) {
                  print(navigation.url);
                  if (navigation.url
                      .startsWith(FuncoesGerais.dominioPermitido)) {
                    return NavigationDecision.navigate;
                  }
                  //print('allowing navigation to $request');
                  launch(navigation.url);
                  return NavigationDecision.prevent;
                },
              ),
      ),
    );
  }

  Widget _buildError() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(msg),
          ElevatedButton(
              onPressed: () {
                this.error = false;
                this.msg = '';
                setState(() {});
              },
              child: Text('Atualizar'))
          // RaisedButton(onPressed: (){
          //   this.error = false;
          //   this.msg = '';
          //   setState(() {
          //   });
          // }, child: Text('Atualizar'),)
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
