import 'package:flutter/material.dart';
import 'package:initial_folder/models/detail_course_model.dart';
import 'package:initial_folder/providers/tab_provider.dart';
import 'package:initial_folder/screens/detail_course/components/aktifitas.dart';
import 'package:initial_folder/screens/detail_course/components/deskripsi.dart';
import 'package:initial_folder/screens/detail_course/components/tab_bar_items.dart';
import 'package:initial_folder/screens/detail_course/components/terkait.dart';
import 'package:initial_folder/screens/detail_course/components/ulasan.dart';
import 'package:initial_folder/size_config.dart';
import 'package:provider/provider.dart';

class CustomTabBar extends StatefulWidget {
  const CustomTabBar({
    Key? key,
    required this.dataDetailCourseModel,
    this.totalDuration,
    this.bio,
    this.instructor,
    this.rating,
    this.review,
    this.totalLesson,
    this.totalStudent,
    this.fotoProfile,
    this.headline,
    this.resoaktifitas,
    this.idCategory,
  }) : super(key: key);
  final DataDetailCourseModel dataDetailCourseModel;
  final String? totalDuration,
      bio,
      instructor,
      review,
      totalLesson,
      totalStudent,
      fotoProfile,
      rating,
      headline,
      resoaktifitas,
      idCategory;

  @override
  State<CustomTabBar> createState() => _CustomTabBarState();
}

class _CustomTabBarState extends State<CustomTabBar> {
  @override
  Widget build(BuildContext context) {
    TabProvider tab = Provider.of<TabProvider>(context);

    void _onHorizontalDragEnd(DragEndDetails details) {
      double velocity = details.primaryVelocity ?? 0.0;
      if (velocity < 0) {
        if (tab.currentIndex < 3) {
          tab.currentIndex++;
        }
      } else if (velocity > 0) {
        if (tab.currentIndex > 0) {
          tab.currentIndex--;
        }
      }
    }

    Widget buildContent(int currentIndex) {
      switch (currentIndex) {
        case 0:
          return Desksripsi(
            dataDetailCourseModel: widget.dataDetailCourseModel,
            video: true,
            id: widget.dataDetailCourseModel.id,
            instructor: widget.instructor,
            bio: widget.bio,
            rating: widget.rating,
            fotoProfile: widget.fotoProfile,
            review: widget.review,
            totalLesson: widget.totalLesson,
            totalStudent: widget.totalStudent,
            headline: widget.headline,
          );
        case 1:
          return Aktifitas(
            id: widget.dataDetailCourseModel.id,
            totalDuration: widget.totalDuration ?? '',
            resoaktifitas: widget.resoaktifitas,
          );
        case 2:
          return Ulasan(
            id: widget.dataDetailCourseModel.id,
          );

        case 3:
          return Terkait(
            idCategory: widget.idCategory,
          );
        default:
          return Desksripsi(
            dataDetailCourseModel: widget.dataDetailCourseModel,
            video: true,
            id: widget.dataDetailCourseModel.id,
            instructor: widget.instructor,
            bio: widget.bio,
            rating: widget.rating,
            fotoProfile: widget.fotoProfile,
            review: widget.review,
            totalLesson: widget.totalLesson,
            totalStudent: widget.totalStudent,
            headline: widget.headline,
          );
      }
    }

    return Column(
      children: [
        Container(
          width: SizeConfig.screenWidth,
          height: getProportionateScreenHeight(29),
          decoration: BoxDecoration(
            color: Theme.of(context).colorScheme.background,
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.1),
                spreadRadius: -12,
                blurRadius: 8,
                offset: Offset(0, getProportionateScreenHeight(12)),
              ),
            ],
          ),
          child: Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                GestureDetector(
                  onTap: () => tab.currentIndex = 0,
                  child: TabBarItems(
                    index: 0,
                    title: 'Deskripsi',
                  ),
                ),
                GestureDetector(
                  onTap: () => tab.currentIndex = 1,
                  child: TabBarItems(
                    index: 1,
                    title: 'Aktivitas',
                  ),
                ),
                GestureDetector(
                  onTap: () => tab.currentIndex = 2,
                  child: TabBarItems(
                    index: 2,
                    title: 'Ulasan',
                  ),
                ),
                GestureDetector(
                  onTap: () => tab.currentIndex = 3,
                  child: TabBarItems(
                    index: 3,
                    title: 'Terkait',
                  ),
                ),
              ],
            ),
          ),
        ),
        GestureDetector(
          onHorizontalDragEnd: _onHorizontalDragEnd,
          child: buildContent(tab.currentIndex),
        ),
      ],
    );
  }
}
