import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/brand_view_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/final_products_page.dart';
import 'package:hmg_patient_app/pages/pharmacies/search_brands_page.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/others/network_base_view.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';

class ProductBrandsPage extends StatefulWidget {
  @override
  _ProductBrandsPageState createState() => _ProductBrandsPageState();
}

var languageID;

class _ProductBrandsPageState extends State<ProductBrandsPage> {
  @override
  void initState() {
    super.initState();
    getLanguageID();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<BrandsViewModel>(
      allowAny: true,
      onModelReady: (model) => model.getBrandsData(),
      builder: (_, model, wi) => AppScaffold(
        appBarTitle: TranslationBase.of(context).brands,
        isShowAppBar: true,
        isPharmacy: true,
        isShowDecPage: false,
        body: SingleChildScrollView(
          child: NetworkBaseView(
            baseViewModel: model,
            isLocalLoader:true,
            child: Container(
              child: Column(
                children: [
                  Container(
                    color: Colors.white,
                    alignment: languageID == 'ar'
                        ? Alignment.topRight
                        : Alignment.topLeft,
                    padding: languageID == 'ar'
                        ? EdgeInsets.only(right: 10.0, top: 10.0)
                        : EdgeInsets.only(left: 10.0, top: 10.0),
                    child: Text(
                      TranslationBase.of(context).topBrands,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  Container(
                    height: 220,
                    width: double.infinity,
                    color: Colors.white,
                    child: topBrand(context),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height * 0.076,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5.0),
                      color: Colors.white,
                    ),
                    child: topBrand(context),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height * 0.066,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5.0),
                      color: Colors.white,
                    ),
                    child: InkWell(
                      child: Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Row(
                          //crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Icon(Icons.search, size: 25.0),
                            SizedBox(
                              width: 15.0,
                            ),
                            Texts(
                              TranslationBase.of(context).searchProductHere,
                              fontSize: 13,
                            )
                          ],
                        ),
                      ),
                      onTap: () {
                        Navigator.push(
                          context,
                          FadePage(page: SearchBrandsPage()),
                        );
                      },
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    height: 250,
                    width: double.infinity,
                    color: Colors.white,
                    child: ListView.builder(
                        itemCount: model.brandsListList.length,
                        itemBuilder: (BuildContext context, int index) {
                          return InkWell(
                            child: Container(
                              margin: EdgeInsets.only(top: 50, left: 10),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  languageID == 'ar'
                                      ? Text(model.brandsListList[index].namen!)
                                      : Text(model.brandsListList[index].name!),
                                  SizedBox(
                                    height: 3,
                                  ),
                                  Divider(height: 1, color: Colors.grey)
                                ],
                              ),
                            ),
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => FinalProductsPage(
                                          id: model.brandsListList[index].id
                                              .toString(),
                                        )),
                              );
                            },
                          );
                        }),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

topBrand(BuildContext context) {
  return BaseView<BrandsViewModel>(
    allowAny: true,
    onModelReady: (model) => model.getTopBrandsData(),
    builder: (_, model, wi) => GridView.count(
      crossAxisCount: 4,
      children: List.generate(
        model.topBrandsListList.length,
        (index) => Column(
          children: [
            InkWell(
              child: Container(
                margin: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 0.0),
                child: Container(
                  child: Container(
                    child: Container(
//                    padding: EdgeInsets.only(left: 5),
                      child: Container(
                          padding: EdgeInsets.symmetric(
                              horizontal: 10.0, vertical: 10.0),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10),
                            border: Border(
                              top: BorderSide(width: 1.0, color: Colors.grey),
                              left: BorderSide(width: 1.0, color: Colors.grey),
                              right: BorderSide(width: 1.0, color: Colors.grey),
                              bottom:
                                  BorderSide(width: 1.0, color: Colors.grey),
                            ),
                            color: Colors.white,
                          ),
                          child: model.topBrandsListList[index].image != null
                              ? Image.network(
                                  model.topBrandsListList[index].image!.src!,
                                  fit: BoxFit.cover,
                                  width: 60,
                                  height: 40,
                                )
                              : Text(model.topBrandsListList[index].name!)),
                    ),
                  ),
                ),
              ),
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => FinalProductsPage(
                            id: model.topBrandsListList[index].id.toString(),
                          )),
                );
              },
            ),
          ],
        ),
      ),
    ),
  );
}

brandSearch() {
  return Text('ENAD');
}

class SearchBar extends SearchDelegate<String> {
  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = "";
        },
      )
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: AnimatedIcon(
        icon: AnimatedIcons.menu_arrow,
        progress: transitionAnimation,
      ),
      onPressed: () {
        close(context, "");
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Container(
      height: 100,
      width: 100,
      child: Card(
        color: Colors.red,
        child: Center(
          child: Text(query),
        ),
      ),
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return ListView.builder(
      itemCount: 5,
      itemBuilder: (context, index) => ListTile(
        leading: Icon(Icons.location_city),
        title: Text("Enad"),
        onTap: () {
          showResults(context);
        },
      ),
    );
  }
}

getLanguageID() async {
  languageID = await sharedPref.getString(APP_LANGUAGE);
}
