import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/constants/assets.dart';
import 'package:tm_app/core/theme/colors.dart';
import 'package:tm_app/core/utils/size_config.dart';
import 'package:tm_app/view_models/your_tenders_view_model.dart';
import 'package:tm_app/views/shared/base_button.dart';
import 'package:tm_app/views/your_tenders/strings/your_tenders_strings.dart';

class YourTendersFilterDialog extends StatelessWidget {
  const YourTendersFilterDialog({super.key});

  Widget _dateRangePill(BuildContext context) {
    final viewModel = context.watch<YourTendersViewModel>();
    final start = viewModel.selectedDateRange?.start;
    final end = viewModel.selectedDateRange?.end;

    Future<void> pick() async {
      final picked = await showDateRangePicker(
        context: context,
        firstDate: DateTime(2020),
        lastDate: DateTime(2035),
        initialDateRange: viewModel.selectedDateRange,
      );
      if (picked != null) {
        viewModel.setDateRange(picked);
      }
    }

    return InkWell(
      borderRadius: BorderRadius.circular(12),
      onTap: pick,
      child: Container(
        height: 56.0.h(),
        padding: const EdgeInsets.symmetric(horizontal: 20),
        decoration: BoxDecoration(
          color: AppColors.grey0,
          borderRadius: BorderRadius.circular(12),
          border: Border.all(color: AppColors.grey40),
        ),
        child: Center(
          child: Stack(
            alignment: Alignment.center,
            children: [
              Align(
                alignment: Alignment.center,
                child: Container(
                  width: 1,
                  margin: const EdgeInsets.symmetric(vertical: 8),
                  color: AppColors.grey40,
                ),
              ),
              Align(
                alignment: Alignment.center,
                child: SizedBox(
                  width: 26.0.w(),
                  height: 26.0.h(),
                  child: SvgPicture.asset(AssetsManager.trailingIcon),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    start == null
                        ? YourTendersStrings.startDate
                        : viewModel.startDateFormatted,
                    style: TextStyle(
                      fontSize: 16.0.sp(),
                      fontWeight: FontWeight.w400,
                      color: AppColors.grey70,
                    ),
                  ),
                  Text(
                    end == null
                        ? YourTendersStrings.endDate
                        : viewModel.endDateFormatted,
                    style: TextStyle(
                      fontSize: 16.0.sp(),
                      fontWeight: FontWeight.w400,
                      color: AppColors.grey70,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<YourTendersViewModel>(
      builder: (context, vm, child) {
        return SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Center(
                  child: Text(
                    YourTendersStrings.filter,
                    style: TextStyle(
                      fontSize: 20.0.sp(),
                      fontWeight: FontWeight.w700,
                      color: AppColors.grey70,
                    ),
                  ),
                ),
                SizedBox(height: 16.0.h()),
                Text(
                  YourTendersStrings.selectPublicationRangeDate,
                  style: TextStyle(
                    fontSize: 16.0.sp(),
                    fontWeight: FontWeight.w600,
                    color: AppColors.grey80,
                  ),
                ),
                SizedBox(height: 20.0.h()),
                _dateRangePill(context),
                SizedBox(height: 16.0.h()),
                Text(
                  YourTendersStrings.status,
                  style: TextStyle(
                    fontSize: 16.0.sp(),
                    fontWeight: FontWeight.w600,
                    color: AppColors.grey70,
                  ),
                ),
                ...[
                  YourTendersStrings.all,
                  YourTendersStrings.likeTenders,
                  YourTendersStrings.dislikeTenders,
                  YourTendersStrings.approvedTenders,
                  YourTendersStrings.tendersSubmitted,
                ].map((status) {
                  return RadioListTile(
                    value: status,
                    groupValue: vm.selectedStatus,
                    activeColor: AppColors.mainBlue,
                    onChanged: (value) => vm.setStatus(value!),
                    title: Text(
                      status,
                      style: TextStyle(
                        fontSize: 16.0.sp(),
                        fontWeight: FontWeight.w400,
                        color: AppColors.grey70,
                      ),
                    ),
                  );
                }),
                SizedBox(height: 16.0.h()),
                SizedBox(
                  width: double.infinity,
                  child: BaseButton(
                    isEnabled: true,
                    onPressed: () {
                      context.pop();
                      vm.callWithFilter();
                    },
                    text: YourTendersStrings.confirm,
                    textColor: AppColors.textBlue,
                    backgroundColor: AppColors.primary20,
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
