import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class ModulesPage extends GetView<ModulesLogic> {
  const ModulesPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          spacing: 5.w,
          children: [
            Text('سامانه جامع رصدیار', style: AppFonts.yekan18Bold.copyWith(color: Colors.white)),
            Assets.logos.finalLogo.image(width: 40.w, height: 40.h),
          ],
        ),
        centerTitle: true,
        backgroundColor: AppColor.blueNormal,
      ),
      body: ObxValue((loading) {
        if (!controller.isLoading.value) {
          return buildMainWidget();
        }
        return buildLoading();
      }, controller.isLoading),
    );
  }

  Container buildLoading() {
    return Container(
        color: Colors.grey.withValues(alpha: 0.5),
        child: Center(child: CupertinoActivityIndicator(color: AppColor.greenNormal, radius: 30)),
      );
  }

  Column buildMainWidget() {
    return Column(
          children: [
            SizedBox(height: 12.h,),
            SliderWidget(widgetTag: "up"),
            SizedBox(height: 12.h,),
            SizedBox(
              height: 278.h,
              child: GridView.builder(
                padding: EdgeInsetsGeometry.symmetric(horizontal: 16.w),
                itemBuilder: (context, index) {
                  final module = controller.moduleList[index];
                  return CardIcon(
                    title: module.title,
                    svgIcon: module.icon,
                    borderColor: module.borderColor,
                    backgroundColor: module.backgroundColor,
                    titleColor: module.titleColor,
                    onTap: () => controller.onTapCard(module.module, index),
                  );
                },
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  mainAxisSpacing: 22.h,
                  crossAxisSpacing: 16.w,
                ),
                physics: NeverScrollableScrollPhysics(),
                itemCount: controller.moduleList.length,
              ),
            ),
            Container(
              height: 107.h,
              margin: EdgeInsets.symmetric(horizontal: 16.w),
              padding: EdgeInsets.fromLTRB(11.w, 8.h, 8.w, 12.h),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment(0.00, 0.50),
                  end: Alignment(1.00, 0.50),
                  colors: [const Color(0xFFC9D5FF), Colors.white],
                ),
                borderRadius: BorderRadius.circular(8),
                border: Border.all(width: 1.w, color: const Color(0xFFD3D3D3)),
              ),
              child: Row(
                spacing: 11.w,
                children: [
                  Expanded(
                    flex: 2,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'آخرین اخبار ',
                          textAlign: TextAlign.right,
                          style: AppFonts.yekan16.copyWith(
                            color: Color(0xFF5B5B5B),
                            height: 1.90,
                          ),
                        ),
                        ObxValue(
                          (data) => Text(
                            data.value ??
                                'اخبار مربوط به جوجه ریزی استان از آخرین روند مطلع شوید...',
                            maxLines: 2,
                            style: AppFonts.yekan12.copyWith(
                              color: Color(0xFF5B5B5B),
                              height: 1.5,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          controller.latestNews,
                        ),
                      ],
                    ),
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Container(
                        width: 103.w,
                        height: 24.h,
                        alignment: Alignment.center,
                        decoration: ShapeDecoration(
                          color: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(50.r),
                          ),
                        ),
                        child: Text(
                          'اطلاعات بیشتر',
                          textAlign: TextAlign.right,
                          style: AppFonts.yekan14,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            SizedBox(height: 12.h,),
            SliderWidget(height: 160, widgetTag: "down"),
          ],
        );
  }
}
