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 '/app/route.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../app/constant.dart';
import '../../models/auth_model.dart';
import '../../provider/current_user_provider.dart';

class SplashScreen extends HookConsumerWidget {
  const SplashScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // setelah 3 detik redirect ke login route
    // Timer(const Duration(seconds: 3), () {
    //   Navigator.of(context).pushNamedAndRemoveUntil(
    //     loginRoute,
    //     (route) => false,
    //   );
    // });
    useEffect(() {
      () async {
        final shared = await SharedPreferences.getInstance();
        bool remeberMe = shared.getBool("isRememberMeX") ?? false;

        if (remeberMe) {
          final jsonAuth = shared.get(Constant.tokenName).toString();
          final auth = jsonDecode(jsonAuth);
          if (auth == null) return;
          final authmodel = AuthModel(
            token: auth['model']['token'],
            model: AuthKurirModel.fromJson(auth['model']),
          );
          final token = authmodel.token;
          final authKurirModel = authmodel.model;

          final authModel = AuthModel(
            token: token,
            model: authKurirModel,
          );

          ref.read(currentUserProvider.notifier).state = authModel;
          Navigator.of(context)
              .pushNamedAndRemoveUntil(menuRoute, (route) => false);
          return;
        } else {
          Navigator.of(context)
              .pushNamedAndRemoveUntil(loginRoute, (route) => false);
          return;
        }
      }();
    }, []);

    return Scaffold(
      backgroundColor: Constant.backgroundWhite,
      body: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.only(
                  top: Constant.getActualY(context: context, y: 103),
                  left: Constant.getActualX(context: context, x: 69),
                  right: Constant.getActualX(context: context, x: 70),
                  bottom: Constant.getActualY(context: context, y: 92)),
              child: SizedBox(
                width: Constant.getActualX(context: context, x: 251),
                height: Constant.getActualY(context: context, y: 78),
                child: Image.asset(
                  "assets/logo_kdr.png",
                  fit: BoxFit.fitHeight,
                  // scale: 1,
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(
                left: Constant.getActualX(context: context, x: 30),
                right: Constant.getActualX(context: context, x: 29),
              ),
              child: SizedBox(
                width: Constant.getActualX(context: context, x: 331),
                height: Constant.getActualY(context: context, y: 366),
                child: Image.asset(
                  "assets/bg_splashscreen.png",
                  fit: BoxFit.fitHeight,
                  // scale: 1,
                ),
                // color: Colors.green,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
