import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/review_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:rating_bar/rating_bar.dart';

class MyReviewsPage extends StatefulWidget {
  @override
  _MyReviewsPageState createState() => _MyReviewsPageState();
}

var languageID;

class _MyReviewsPageState extends State<MyReviewsPage> {
  void initState() {
    getLanguageID();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<ReviewViewModel>(
      onModelReady: (model) => model.getReviewData(),
      builder: (_, model, wi) => AppScaffold(
        appBarTitle: TranslationBase.of(context).reviews,
        isShowAppBar: true,
        isPharmacy: true,
        showPharmacyCart: false,
        showHomeAppBarIcon: false,
        isBottomBar: true,
        baseViewModel: model,
        body: model.reviewListList.length == 0
            ? Container(
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Image.asset(
                          'assets/images/new-design/empty_box.png',
                          width: 100,
                          height: 100,
                          fit: BoxFit.cover,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          TranslationBase.of(context).noData,
//                          'There is no data',
                          style: TextStyle(fontSize: 30),
                        ),
                      )
                    ],
                  ),
                ),
              )
            : Container(
                child: ListView.builder(
                    itemCount: model.reviewListList.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Column(
                        children: [
                          Container(
                            child: reviewDetails(
                              model.reviewListList[index],
                              double.parse(model.reviewListList[index].product.approvedTotalReviews.toString()),
                              double.parse(model.reviewListList[index].rating.toString()),
                            ),
                          ),
                          Divider(height: 1, color: Colors.grey)
                        ],
                      );
                    }),
              ),
      ),
    );
  }
}

reviewDetails(data, rate, myRate) {
  return Container(
    child: Padding(
      padding: EdgeInsets.only(bottom: 10.0),
      child: Container(
        height: 200,
        width: double.infinity,
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Row(
              children: [
                Container(
                  margin: languageID == 'ar' ? EdgeInsets.only(top: 10, right: 10) : EdgeInsets.only(top: 10, left: 10),
                  child: Image.network(
                    data.product.images[0].src.trim(),
                    fit: BoxFit.cover,
                    width: 80,
                    height: 80,
                  ),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Container(
                      margin: EdgeInsets.all(5),
                      child: Align(
                        alignment: languageID == 'ar' ? Alignment.topRight : Alignment.topLeft,
                        child: RichText(
                          text: TextSpan(
                            text: languageID == 'ar' ? data.product.namen : data.product.name,
                            style: TextStyle(color: Colors.black54, fontSize: languageID == 'ar' ? 12 : 13, fontWeight: FontWeight.bold),
                          ),
                        ),
                      ),
                    ),
                    Column(
                      children: [
                        Container(
                          margin: EdgeInsets.all(5),
                          child: Align(
                            alignment: languageID == 'ar' ? Alignment.topRight : Alignment.topLeft,
                            child: RichText(
                              text: TextSpan(
                                text: data.product.price.toString() + " " + data.product.currency,
                                style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black, fontSize: 13),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.all(5),
                      child: Align(
                        alignment: languageID == 'ar' ? Alignment.topRight : Alignment.topLeft,
                        child: RatingBar.readOnly(
                          initialRating: rate,
                          size: 15.0,
                          filledColor: Colors.yellow[700],
                          emptyColor: Colors.grey[500],
                          isHalfAllowed: true,
                          halfFilledIcon: Icons.star_half,
                          filledIcon: Icons.star,
                          emptyIcon: Icons.star,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            SizedBox(
              height: 20,
            ),
            Container(
              child: Row(
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Container(padding: languageID == 'ar' ? EdgeInsets.only(right: 10) : EdgeInsets.only(left: 10), child: Text(data.createdOnUtc.toString().substring(0, 11))),
                      ],
                    ),
                  ),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Container(
                          padding: languageID == 'ar' ? EdgeInsets.only(right: 60) : EdgeInsets.only(left: 60),
                          child: RatingBar.readOnly(
                            initialRating: myRate,
                            size: 15.0,
                            filledColor: Colors.yellow[700],
                            emptyColor: Colors.grey[500],
                            isHalfAllowed: true,
                            halfFilledIcon: Icons.star_half,
                            filledIcon: Icons.star,
                            emptyIcon: Icons.star,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 15,
            ),
            Expanded(
              child: Container(
                padding: languageID == 'ar' ? EdgeInsets.only(right: 10) : EdgeInsets.only(left: 10),
                child: Text(getReview(data) ?? ""),
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

String getReview(data) {
  String reviewTxt = "";

  if (data.reviewText == null && data.replyText == null) {
    reviewTxt = "";
  }
  if (data.reviewText == null)
    reviewTxt = data.replyText;
  else
    reviewTxt = data.reviewText;

  return reviewTxt;
}

fixingString(txt) {
  String stringTxt;
  String newTxt;
  stringTxt = txt.toString();
  newTxt = stringTxt.split('.')[1];

  return newTxt;
}

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