import 'package:flutter/material.dart';

import '../controls/linear_status_indicator.dart';
import '../controls/standard_appbar.dart';
import '../controls/timeline/timeline_panel.dart';
import '../globals.dart';
import '../models/TimelineIdentifiers.dart';
import '../services/network_status_service.dart';
import '../services/timeline_manager.dart';
import '../utils/active_profile_selector.dart';

class UserPostsScreen extends StatefulWidget {
  final String userId;

  const UserPostsScreen({super.key, required this.userId});

  @override
  State<UserPostsScreen> createState() => _UserPostsScreenState();
}

class _UserPostsScreenState extends State<UserPostsScreen> {
  late final TimelineIdentifiers timeline;

  @override
  void initState() {
    super.initState();
    timeline = TimelineIdentifiers.profile(widget.userId);
    getIt<ActiveProfileSelector<TimelineManager>>()
        .activeEntry
        .andThenSuccess((m) => updateTimeline(m));
  }

  void updateTimeline(TimelineManager manager) {
    Future.delayed(const Duration(milliseconds: 100), () async {
      await manager.updateTimeline(timeline, TimelineRefreshType.refresh);
    });
  }

  @override
  Widget build(BuildContext context) {
    final nss = getIt<NetworkStatusService>();

    return Scaffold(
      appBar: StandardAppBar.build(
        context,
        'User Posts',
        actions: [],
      ),
      body: Center(
        child: Column(
          children: [
            StandardLinearProgressIndicator(nss.timelineLoadingStatus),
            Expanded(
              child: TimelinePanel(
                timeline: timeline,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
