import 'dart:async';
import 'package:didvan/models/requests/studio.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/services/media/media.dart';
import 'package:didvan/views/home/media/widgets/featured_podcast_card.dart';
import 'package:didvan/views/home/media/widgets/podcast_list_card.dart'; 
import 'package:didvan/views/podcasts/podcasts_state.dart';
import 'package:didvan/views/widgets/item_title.dart';
// import 'package:didvan/views/widgets/overview/podcast.dart'; 
import 'package:didvan/views/widgets/state_handlers/empty_result.dart';
import 'package:didvan/views/widgets/state_handlers/state_handler.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

class PodcastTabPage extends StatefulWidget {
  const PodcastTabPage({super.key});

  @override
  State<PodcastTabPage> createState() => _PodcastTabPageState();
}

class _PodcastTabPageState extends State<PodcastTabPage> {
  int _currentFeaturedIndex = 0;
  Timer? _rotationTimer;
  bool _isPlaying = false;

  @override
  void initState() {
    super.initState();
    Future.microtask(() {
      context.read<PodcastsState>().init(true);
      context.read<PodcastsState>().getStudios(page: 1);
    });
  }

  void _startRotation() {
    _rotationTimer?.cancel();
    _rotationTimer = Timer.periodic(const Duration(seconds: 5), (timer) {
      final state = context.read<PodcastsState>();
      if (state.studios.isNotEmpty && mounted && !_isPlaying) {
        setState(() {
          _currentFeaturedIndex =
              (_currentFeaturedIndex + 1) % state.studios.length;
        });
      }
    });
  }

  void _onPlayStateChanged(bool isPlaying) {
    setState(() {
      _isPlaying = isPlaying;
    });
  }

  void _showSortDialog() {
    final state = context.read<PodcastsState>();
    showModalBottomSheet(
      backgroundColor: Colors.white,
      context: context,
      builder: (context) {
        return Container(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ListTile(
                title: const Text('تازه‌ترین‌ها'),
                onTap: () {
                  state.selectedSortTypeIndex = 0;
                  state.getStudios(page: 1);
                  Navigator.pop(context);
                },
              ),
              ListTile(
                title: const Text('قدیمی‌ترین‌ها'),
                onTap: () {
                  state.selectedSortTypeIndex = 1;
                  state.getStudios(page: 1);
                  Navigator.pop(context);
                },
              ),
              ListTile(
                title: const Text('پربازدیدترین‌ها'),
                onTap: () {
                  state.selectedSortTypeIndex = 2;
                  state.getStudios(page: 1);
                  Navigator.pop(context);
                },
              ),
              ListTile(
                title: const Text('پربحث‌ترین‌ها'),
                onTap: () {
                  state.selectedSortTypeIndex = 3;
                  state.getStudios(page: 1);
                  Navigator.pop(context);
                },
              ),
            ],
          ),
        );
      },
    );
  }

  void _navigateToDetails(int index) {
    final state = context.read<PodcastsState>();
    final item = state.studios[index];

    Navigator.pushNamed(
      context,
      Routes.studioDetails,
      arguments: {
        'id': item.id,
        'type': item.type,
        'onMarkChanged': (int id, bool value, [bool shouldUpdate = true]) {
          context.read<PodcastsState>().changeMark(id, value, shouldUpdate);
        },
        'args': StudioRequestArgs(
          page: state.page,
          type: state.type,
          search: state.search,
          order: state.order,
          asc: state.selectedSortTypeIndex == 1,
          endDate: state.endDate?.split(' ').first,
          startDate: state.startDate?.split(' ').first,
        ),
      },
    );
  }


  @override
  void dispose() {
    _rotationTimer?.cancel();
    if (MediaService.isPlayingFromFeaturedCard && MediaService.audioPlayer.playing) {
      MediaService.audioPlayer.pause();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final state = context.watch<PodcastsState>();

    return StateHandler<PodcastsState>(
      state: state,
      emptyState: EmptyResult(
        onNewSearch: () {},
      ),
      enableEmptyState: state.studios.isEmpty,
      builder: (context, state) {
        if (state.studios.isEmpty) {
          return const SizedBox.shrink();
        }

        if (_rotationTimer == null || !_rotationTimer!.isActive) {
          WidgetsBinding.instance.addPostFrameCallback((_) {
            _startRotation();
          });
        }

        return SingleChildScrollView(
          child: Column(
            children: [
              AnimatedSwitcher(
                duration: const Duration(milliseconds: 500),
                transitionBuilder: (Widget child, Animation<double> animation) {
                  return FadeTransition(opacity: animation, child: child);
                },
                child: FeaturedPodcastCard(
                  key: ValueKey<int?>(state.studios[_currentFeaturedIndex].id),
                  podcast: state.studios[_currentFeaturedIndex],
                  onTap: () => _navigateToDetails(_currentFeaturedIndex),
                  onPlayStateChanged: _onPlayStateChanged,
                ),
              ),

              if (state.studios.length > 1)
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const ItemTitle(
                        title: 'همه پادکست‌ها',
                        color: Color.fromARGB(255, 0, 53, 70),
                        style: TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 18),
                      ),
                      Row(
                        children: [
                          GestureDetector(
                              onTap: _showSortDialog,
                              child: Text(
                                state.orderString,
                                style: const TextStyle(
                                    color: Color.fromARGB(255, 0, 53, 70)),
                              )),
                          IconButton(
                              onPressed: _showSortDialog,
                              icon: SvgPicture.asset(
                                  'lib/assets/icons/sort2.svg')),
                        ],
                      ),
                    ],
                  ),
                ),

              if (state.studios.length > 1)
                ListView.builder(
                  itemCount: state.studios.length,
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    final podcast = state.studios[index];
                    
                    return PodcastListCard(
                      podcast: podcast,
                      onTap: () => _navigateToDetails(index),
                    );
                  },
                ),
            ],
          ),
        );
      },
      onRetry: () => context.read<PodcastsState>().getStudios(page: 1),
    );
  }
}