import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/global/global_state/global_state.dart';
import 'package:qadirneyriz/setting/setting.dart';
import 'package:qadirneyriz/utils/enums/status.dart';
import 'package:qadirneyriz/widgets/custom_appbar.dart';
import 'package:qadirneyriz/widgets/custom_netimage.dart';

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

  @override
  State<AboutUsScreen> createState() => _AboutUsScreenState();
}

class _AboutUsScreenState extends State<AboutUsScreen> {
  // تابع کمکی برای گرفتن مقدار کلید از داده‌ها
  String _getByKey(GlobalState globalState, String key) {
    if (globalState.logoImagesStatus == Status.ready) {
      return globalState.logoImagesModel?.data
              ?.firstWhere((element) => element.key == key)
              .value ??
          '';
    }
    return '';
  }

  // تابع کمکی برای ساخت URL
  String _buildImageUrl(String imagePath) {
    return '${config.network.imageUrl}$imagePath';
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<GlobalState>(
      builder: (context, globalState, child) {
        // گرفتن مقادیر مربوطه
        final String textAboutUsFarsi =
            _getByKey(globalState, 'about_us_description_fa');
        final String textAboutUsEn =
            _getByKey(globalState, 'about_us_description_en');
        final String textAppVersrionFa =
            _getByKey(globalState, 'app_version_fa');
        final String textAppVersrionEn =
            _getByKey(globalState, 'app_version_en');
        final String aboutUsImage = _getByKey(globalState, 'about_us_image');
        final String logoImage = _getByKey(globalState, 'logo');
        final String aboutUsImageUrl = _buildImageUrl(aboutUsImage);
        final String logoUrl = _buildImageUrl(logoImage);

        // ساخت ویو بر اساس وضعیت
        switch (globalState.logoImagesStatus) {
          case Status.ready:
            return CustomScrollView(
              slivers: [
                const CustomAppbar(),
                SliverToBoxAdapter(
                  child: Column(
                    children: [
                      _buildImageSection(aboutUsImageUrl, 300, 300),
                      _buildAboutUsText(
                          context, textAboutUsFarsi, textAboutUsEn),
                      _buildFooter(
                          logoUrl, textAppVersrionEn, textAppVersrionFa),
                    ],
                  ),
                ),
              ],
            );

          default:
            return const Center(child: CircularProgressIndicator());
        }
      },
    );
  }

  // ویجت برای بخش تصویر
  Widget _buildImageSection(String imageUrl, double width, double height) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 50),
      child: CustomImage(
        image: imageUrl,
        logo: true,
        width: width,
        height: height,
        boxFit: BoxFit.contain,
      ),
    );
  }

  // ویجت برای متن درباره ما
  Widget _buildAboutUsText(BuildContext context, String textFa, textEn) {
    final String lang = setting.userLocalDb.getUser().language;
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: Text(
        lang == 'en' ? textEn : textFa,
        textAlign: TextAlign.justify,
      ),
    );
  }

  // ویجت برای بخش فوتر (لوگو و نسخه اپلیکیشن)
  Widget _buildFooter(
      String logoUrl, String textVersionEn, String textVersionFa) {
    final String lang = setting.userLocalDb.getUser().language;
    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 10),
          child: CustomImage(
            image: logoUrl,
            logo: true,
            width: 80,
            height: 80,
            boxFit: BoxFit.contain,
          ),
        ),
        const SizedBox(height: 8),
        Text(
          lang == 'en' ? textVersionEn : textVersionFa,
          style: TextStyle(fontSize: 12),
        ),
        const SizedBox(height: 10),
      ],
    );
  }
}
