import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';

import '../../../base/base_view.dart';
import '../../../widgets/ix_app_bar.dart';
import '../controllers/markdown_controller.dart';

class MarkdownView extends BaseView<MarkdownController> {
  const MarkdownView({super.key});

  @override
  PreferredSizeWidget? get appBar => IXAppBar(title: controller.title);

  @override
  Widget buildContent(BuildContext context) {
    return _buildMarkdownContent();
  }

  Widget _buildMarkdownContent() {
    return FutureBuilder(
      future: rootBundle.loadString('assets/md/${controller.filename}.md'),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasData) {
            return Markdown(
              physics: const BouncingScrollPhysics(),
              data: snapshot.data,
            );
          }
        }
        return Center(
          child: SizedBox(
            width: 20.w,
            height: 20.w,
            child: CircularProgressIndicator(
              strokeWidth: 2,
              valueColor: AlwaysStoppedAnimation<Color>(
                Get.reactiveTheme.primaryColor,
              ),
            ),
          ),
        );
      },
    );
  }
}
