import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:external_app_launcher/external_app_launcher.dart';
import 'package:fhub/backend/DebugHelper.dart';
import 'package:fhub/home.dart';
import 'package:fhub/src/CustomWidgets.dart';
import 'package:fhub/welcome.dart';
import 'package:flutter/material.dart';

class OfflinePage extends StatefulWidget {
  OfflinePage({super.key,required this.id});
  int id=0;
  @override
  State<OfflinePage> createState() => _OfflinePageState();
}
var subscription;
class _OfflinePageState extends State<OfflinePage> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    kickstartAnimations();

    subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) async{
      // Got a new connectivity status!
      try{
        final connectivityResult = await (Connectivity().checkConnectivity());
        Debug.Log(connectivityResult);
        if(connectivityResult == ConnectivityResult.mobile || connectivityResult == ConnectivityResult.wifi || connectivityResult == ConnectivityResult.ethernet || connectivityResult == ConnectivityResult.vpn){
          Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_)=>MyHomePage()));

          return;
        }
      }catch(e){

      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    subscription.cancel();
  }

  void kickstartAnimations() async {
    await Future.delayed(const Duration(milliseconds: 500));

    setState(() {
      op1 = 1;
      op2 = 1;
      op3 = 1;
    });
  }
  @override
  Widget build(BuildContext context) {
    int id = widget.id;
    // id = ;
    return Scaffold(
        backgroundColor: Colors.black,
        extendBody: true,
        body: CustomBody(
          onAnimEnd: () {
            setState(() {});

          },
          context: context,
          child: SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(24.0),
              child: id == 0 ? NoInternet() : ( id == 1 ? NewUpdate():Maintaince())
            ),
          ),
        ));
  }

  Widget NoInternet(){
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween
      ,children: [
      Center(child: GradientText(text:"No Internet", gradient: LinearGradient(colors: [Colors.white.withOpacity(0.6),Colors.white.withOpacity(0.2)]),style: TextStyle(fontSize: 50,fontWeight: FontWeight.bold),)),
      Column(children:
      [
        Icon(Icons.signal_wifi_connected_no_internet_4,size: 105,color: Colors.white.withOpacity(0.4),),
        Padding(
          padding: const EdgeInsets.all(15.0),
          child: Text("This app need an active internet connection to funtion.\nPlease Connect to Internet to continue.",textAlign: TextAlign.center,),
        )

      ]
      ),
      Column()
    ],);
  }

  Widget NewUpdate(){
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween
      ,children: [
      Center(child: GradientText(text:"New Update!", gradient: LinearGradient(colors: [Colors.white.withOpacity(0.6),Colors.white.withOpacity(0.2)]),style: TextStyle(fontSize: 50,fontWeight: FontWeight.bold),)),
      Column(children:
      [
        Icon(Icons.download_outlined,size: 100,color: Colors.white.withOpacity(0.4),),
        Padding(
          padding: const EdgeInsets.all(15.0),
          child: Text("Please Update to continue.",textAlign: TextAlign.center,),
        ),
        SizedBox(height: 30,),
        GlassButton(onTap: () async{
          await LaunchApp.openApp(
            androidPackageName: "https://play.google.com/store/apps/details?id=com.Xperience.FaucetHub",
            // openStore: false
          );
        }, child: Text("Update",style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold),), width: 200,height: 50,color: Colors.greenAccent)

      ]
      ),
      Column()
    ],);
  }

  Widget Maintaince(){
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween
      ,children: [
      Center(child: GradientText(text:"Under Maintaince!", gradient: LinearGradient(colors: [Colors.white.withOpacity(0.6),Colors.white.withOpacity(0.2)]),style: TextStyle(fontSize: 38,fontWeight: FontWeight.bold),)),
      Column(children:
      [
        Icon(Icons.developer_mode_outlined,size: 100,color: Colors.white.withOpacity(0.4),),
        Padding(
          padding: const EdgeInsets.all(30.0),
          child: Text("Please Check back in later.",textAlign: TextAlign.center,),
        ),

      ]
      ),
      Column()
    ],);
  }
}
