import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mitra_corporate/app/constant.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../app/route.dart';
import '../../model/auth_model.dart';
import '../../provider/auth_provider.dart';

class SplashScreen extends HookConsumerWidget {
  const SplashScreen({super.key});
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    getPref() async {
      final SharedPreferences prefs = await SharedPreferences.getInstance();
      final String? getData = prefs.getString(Constant.tokenName);
      // print(getData);
      if (getData != null) {
        Timer(Duration(seconds: 1), () {
          ref.read(authProvider.notifier).state =
              AuthModel.fromJson(jsonDecode(getData));
          Navigator.pushNamed(context, homeRoute);
        });
      } else {
        Timer(Duration(seconds: 1), () {
          Navigator.pushNamed(context, loginRoute);
        });
      }
    }

    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        // FlutterNativeSplash.remove();
        getPref();
      });
      return () {};
    }, []);
    return Container(
      color: Colors.white,
      height: MediaQuery.of(context).size.height,
      width: MediaQuery.of(context).size.width,
      child: Center(
        child: LoadingAnimationWidget.staggeredDotsWave(
            color: Constant.primaryRed, size: 80),
      ),
    );
  }
}
