import 'package:flutter/material.dart';
import '../theme/app_theme.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../blocs/editor_session/editor_session_bloc.dart';
import '../blocs/editor_session/editor_session_event.dart';
import '../blocs/editor_session/editor_session_state.dart';
import '../services/file_service.dart';
import '../injection.dart';
import 'package:go_router/go_router.dart';

class EditorPage extends StatefulWidget {
  final String orgId;
  final String fileId;

  const EditorPage({super.key, required this.orgId, required this.fileId});

  @override
  State<EditorPage> createState() => _EditorPageState();
}

class _EditorPageState extends State<EditorPage> {
  late EditorSessionBloc _editorBloc;

  @override
  void initState() {
    super.initState();
    _editorBloc = EditorSessionBloc(getIt<FileService>());
    _editorBloc.add(
      EditorSessionStarted(orgId: widget.orgId, fileId: widget.fileId),
    );
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider.value(
      value: _editorBloc,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Document Editor'),
          actions: [
            IconButton(
              icon: const Icon(Icons.close),
              onPressed: () {
                _editorBloc.add(EditorSessionEnded());
                GoRouter.of(
                  context,
                ).go('/viewer/${widget.orgId}/${widget.fileId}');
              },
            ),
          ],
        ),
        body: BlocBuilder<EditorSessionBloc, EditorSessionState>(
          builder: (context, state) {
            if (state is EditorSessionStarting) {
              return const Center(child: CircularProgressIndicator());
            }
            if (state is EditorSessionFailed) {
              return Center(child: Text('Error: ${state.message}'));
            }
            if (state is EditorSessionActive) {
              // Placeholder for Collabora iframe
              return Container(
                color: AppTheme.secondaryText,
                child: Center(
                  child: Text(
                    'Collabora Editor Active\n(URL: ${state.editUrl})',
                    textAlign: TextAlign.center,
                    style: const TextStyle(color: AppTheme.primaryText),
                  ),
                ),
              );
            }
            if (state is EditorSessionReadOnly) {
              return Container(
                color: AppTheme.secondaryText,
                child: Center(
                  child: Text(
                    'Read Only Mode\n(URL: ${state.viewUrl})',
                    textAlign: TextAlign.center,
                    style: const TextStyle(color: AppTheme.primaryText),
                  ),
                ),
              );
            }
            if (state is EditorSessionExpired) {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Text('Editing session expired.'),
                    ElevatedButton(
                      onPressed: () {
                        _editorBloc.add(
                          EditorSessionStarted(
                            orgId: widget.orgId,
                            fileId: widget.fileId,
                          ),
                        );
                      },
                      child: const Text('Reopen'),
                    ),
                  ],
                ),
              );
            }
            return const Center(child: Text('Editor not started'));
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    _editorBloc.close();
    super.dispose();
  }
}
