import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/LastVisitedViewModel.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/others/network_base_view.dart';
import 'package:flutter/material.dart';

import '../../../final_products_page.dart';
import '../ProductTileItem.dart';
import 'ViewAllHomeWidget.dart';

class RecentlyViewedWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BaseView<LastVisitedViewModel>(
        onModelReady: (model) => model.getLastVisitedProducts(),
        allowAny: true,
        builder: (_, model, wi) => NetworkBaseView(
              isLocalLoader: true,
              baseViewModel: model,
              child: model.lastVisitedProducts.isNotEmpty
                  ? Container(
                      child: Column(
                        children: [
                          ViewAllHomeWidget(
                              TranslationBase.of(context).recentlyViewed,
                              FinalProductsPage(
                                id: "",
                                productType: 3,
                              )),
                          if (model.state != ViewState.BusyLocal)
                            Container(
                              height: model.lastVisitedProducts.length > 0
                                  ? MediaQuery.of(context).size.height / 3 + 1
                                  : 0,
                              child: ListView.builder(
                                itemBuilder: (ctx, i) => ProductTileItem(
                                    model.lastVisitedProducts[i],
                                    MediaQuery.of(context).size.height / 4 +
                                        20),
                                scrollDirection: Axis.horizontal,
                                itemCount: model.lastVisitedProducts.length,
                              ),
                            )
                          else
                            Container(
                              height: 80,
                              child: Center(
                                child: CircularProgressIndicator(
                                  backgroundColor: Colors.white,
                                  valueColor: AlwaysStoppedAnimation<Color>(
                                    Colors.grey[500]!,
                                  ),
                                ),
                              ),
                            ),
                        ],
                      ),
                    )
                  : Container(),
            ));
  }
}
