// ignore_for_file: deprecated_member_use, undefined_prefixed_name

import 'dart:ui' as ui;

import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/services/media/media.dart';
import 'package:didvan/views/podcasts/studio_details/studio_details_state.dart';
import 'package:didvan/views/podcasts/studio_details/widgets/studio_details_widget.dart';
import 'package:didvan/views/widgets/bookmark_button.dart';
import 'package:didvan/views/widgets/audio/audio_player_widget.dart';
import 'package:didvan/views/widgets/didvan/app_bar.dart';
import 'package:didvan/views/widgets/state_handlers/state_handler.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:universal_html/html.dart' as html;

class StudioDetails extends StatefulWidget {
  final Map<String, dynamic> pageData;

  const StudioDetails({super.key, required this.pageData});

  @override
  State<StudioDetails> createState() => _StudioDetailsState();
}

class _StudioDetailsState extends State<StudioDetails> {
  @override
  void initState() {
    final state = context.read<StudioDetailsState>();
    state.args = widget.pageData['args'];
    Future.delayed(
      Duration.zero,
      () => state.getStudioDetails(widget.pageData['id']),
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final d = MediaQuery.of(context);
    return Consumer<StudioDetailsState>(
      builder: (context, state, child) => StateHandler<StudioDetailsState>(
        state: state,
        onRetry: () => state.getStudioDetails(state.studio.id),
        builder: (context, state) {
          final String viewType = 'video-iframe-${state.studio.id}';

          if (state.studio.type == 'video') {
            debugPrint("Playing video from URL: ${state.studio.link}");
            
            ui.platformViewRegistry.registerViewFactory(
              viewType,
              (int viewId) => html.IFrameElement()
                ..allowFullscreen = true
                ..src = Uri.dataFromString(
                  '<style>*{padding: 0 ; margin: 0; background: black;}</style>${state.studio.iframe!}',
                  mimeType: 'text/html',
                ).toString()
                ..style.border = 'none',
            );
          }
          return WillPopScope(
            onWillPop: () async {
              if (MediaService.currentPodcast != null) {
                state.studio = MediaService.currentPodcast!;
              }
              state.handleTracking(id: state.studio.id);
              return true;
            },
            child: SafeArea(
              child: Scaffold(
                backgroundColor: Theme.of(context).colorScheme.surface,
                appBar: PreferredSize(
                  preferredSize: const Size.fromHeight(56),
                  child: DidvanAppBar(
                    appBarData: AppBarData(
                      trailing: BookmarkButton(
                        itemId: state.studio.id,
                        type:
                            state.studio.type == 'video' ? 'video' : 'podcast',
                        value: state.studio.marked,
                        onMarkChanged: (value) {
                          widget.pageData['onMarkChanged'](
                              state.studio.id, value);
                        },
                        gestureSize: 48,
                      ),
                      isSmall: true,
                      title: state.studio.title,
                    ),
                  ),
                ),
                body: SingleChildScrollView(
                  physics: const NeverScrollableScrollPhysics(),
                  child: SizedBox(
                    height: d.size.height - d.padding.top - 56,
                    child: Column(
                      children: [
                        if (state.studio.type == 'video')
                          AspectRatio(
                            aspectRatio: 16 / 9,
                            child: HtmlElementView(
                              viewType: viewType,
                              key: ValueKey(state.studio.id),
                            ),
                          ),
                        if (state.studio.type == 'podcast')
                          AudioPlayerWidget(podcast: state.studio),
                        Expanded(
                          child: StudioDetailsWidget(
                            onMarkChanged: (id, value) => widget
                                .pageData['onMarkChanged'](id, value, true),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}