import 'package:app_tracking_transparency/app_tracking_transparency.dart';
import 'package:boilerplate/core/constant/constants.dart';
import 'package:boilerplate/core/di/injection_init.dart';
import 'package:boilerplate/domain/entities/flavor/flavor_data.dart';
import 'package:boilerplate/presentation/bloc/landing/bottom_navigation_cubit.dart';
import 'package:boilerplate/presentation/screen/app_update/app_update.dart';
import 'package:boilerplate/presentation/screen/chat/chat_screen.dart';
import 'package:boilerplate/presentation/screen/home/home_screen.dart';
import 'package:boilerplate/presentation/screen/products/list/product_list_screen.dart';
import 'package:boilerplate/presentation/screen/web_view/web_view_screen.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../core/i18n/translations.g.dart';
import '../../widgets/will_exit_app.dart';
import 'widgets/bottom_navigation.dart';

class LandingScreem extends StatefulWidget {
  LandingScreem({Key? key}) : super(key: key);

  @override
  State<LandingScreem> createState() => _LandingScreemState();
}

class _LandingScreemState extends State<LandingScreem> {
  late final BottomNavigationCubit cubit;
  final PageController controller = PageController();
  late String lang = '';

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

    cubit = BlocProvider.of<BottomNavigationCubit>(context);

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      await AppTrackingTransparency.requestTrackingAuthorization();
    });
    getLang();
  }

  _onIndexChange(int newIndex) {
    cubit.changePage(newIndex);
  }

  @override
  void dispose() {
    controller.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillExitApp(
      child: AppUpdated(
        child: BlocConsumer<BottomNavigationCubit, int>(
          bloc: cubit,
          listener: (context, state) {
            controller.animateToPage(
              state,
              duration: Duration(milliseconds: 200),
              curve: Curves.easeIn,
            );
          },
          builder: (context, state) => Column(
            children: [
              Expanded(
                child: PageView(
                  physics: NeverScrollableScrollPhysics(),
                  controller: controller,
                  children: [
                    HomeScreen(),
                    ProductListScreen(),
                    WebViewScreen(
                        url:
                            "https://ipmart.ipthailand.go.th/me?tabs=tabs-2#tabs-2&lang=${lang}",
                        isLanding: true,
                        title: lang == 'th' || lang == ""
                            ? AppLocalizations.landing.business_matching
                            : AppLocalizations.landing.business_matching_en),
                    WebViewScreen(
                      title: lang == 'th' || lang == ""
                          ? AppLocalizations.landing.chat
                          : AppLocalizations.landing.chat_en,
                      url:
                          '${getIt.get<AppFlavorData>().webDomain}/mobile-chatbot?mobile=1&lang=${lang}&bg-white=1',
                    ),
                    // ChatScreen(),
                    WebViewScreen(
                      url: getIt.get<AppFlavorData>().webDomain +
                          '$WEB_PROFILE' +
                          '&lang=${lang}',
                      isLanding: true,
                      title: lang == 'th' || lang == ""
                          ? AppLocalizations.landing.profile
                          : AppLocalizations.landing.profile_en,
                    ),
                  ],
                ),
              ),
              Container(
                color: Colors.white,
                child: BlocProvider.value(
                  value: cubit,
                  child: BottomNavigation(
                    currentIndex: state,
                    myLang: lang,
                    onIndexChanged: _onIndexChange,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  getLang() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    setState(() {
      lang = prefs.getString('lang').toString();
    });
    await FirebaseAnalytics.instance
        .logScreenView(screenName: 'SplashScreenPage');
  }
}
