import 'dart:ui' as ui show ParagraphBuilder, PlaceholderAlignment;

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_ranking_list/model/hot_ranking_list_data_model.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_ranking_list/model/hot_ranking_list_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class HotRankingGoods extends StatelessWidget {
  Good good;
  HotRankingListModel styleModel;
  int index;

  HotRankingGoods({Key key, this.good, this.styleModel, this.index})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var indexImage;
    if (styleModel.hotRankIconList != null &&
        styleModel.hotRankIconList.length > 0 &&
        styleModel.hotRankIconList.length - 1 >= index) {
      indexImage = styleModel.hotRankIconList[index];
    }
    Providers providers = getProvider(good.provider);
    return GestureDetector(
      onTap: () {
        RouterUtil.route(SkipModel(skipIdentifier: "goods_details"),
            good?.toJson(), context);
      },
      child: Stack(
        children: <Widget>[
          Container(
            padding: EdgeInsets.all(15.w),
            margin:
                EdgeInsets.only(top: 8.w, bottom: 8.w, left: 25.w, right: 25.w),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(15.w)),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                /// 商品图片
                Container(
                  width: 254.w,
                  height: 254.w,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(6),
                    child: Hero(
                      tag: (good?.goodImage ?? "") + "top",
                      child: CachedNetworkImage(
                        imageUrl: good?.goodImage ?? '',
                      ),
                    ),
                  ),
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(6)),
                ),

                /// 商品图片右边视图
                Expanded(
                    child: Container(
                  margin: EdgeInsets.only(left: 20.w),
                  child: Column(
                    // mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      /// 商品标题
                      RichText(
                          textAlign: TextAlign.start,
                          overflow: TextOverflow.ellipsis,
                          maxLines: 2,
                          text: TextSpan(children: <InlineSpan>[
                            WidgetSpan(
                                alignment: ui.PlaceholderAlignment.middle,
                                child: Container(
                                  padding: EdgeInsets.only(
                                      left: 4.w, right: 4.w, top: 1, bottom: 1),
                                  decoration: BoxDecoration(
                                      color: HexColor.fromHex(
                                          providers.providerBgColor),
                                      borderRadius: BorderRadius.circular(2.5)),
                                  child: Text(
                                    good.providerName ?? "",
                                    style: TextStyle(
                                        color: HexColor.fromHex(
                                          providers.providerNameColor,
                                        ),
                                        fontSize: 18.sp),
                                  ),
                                )),
                            WidgetSpan(
                                child: SizedBox(
                              width: 4.h,
                            )),
                            TextSpan(
                                text: good.goodTitle,
                                style: TextStyle(
                                    color: HexColor.fromHex(
                                        styleModel.titleColor ?? ""),
                                    fontSize: 30.sp))
                          ])),

                      /// 优惠券
                      Row(
                        children: <Widget>[
                          good.coupon == ""
                              ? Container()
                              : Container(
                                  margin: EdgeInsets.only(
                                      top: 4, bottom: 4, right: 15.w),
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(2.5),
                                      color: HexColor.fromHex(styleModel
                                          .couponCommission.left.couponBgColor),
                                      image: DecorationImage(
                                          image: CachedNetworkImageProvider(
                                              styleModel.couponCommission.left
                                                  .couponBgImg))),
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        left: 8, right: 8, top: 2, bottom: 2),
                                    child: Text(
                                      (good.coupon ?? "") +
                                          (styleModel.couponCommission.left
                                                  .couonText ??
                                              ""),
                                      style: TextStyle(
                                          color: HexColor.fromHex(styleModel
                                              .couponCommission
                                              .left
                                              .couponFontColor),
                                          fontSize: 22.sp,
                                          fontFamily: 'Din',
                                          package: 'zhiying_base_widget'),
                                    ),
                                  ),
                                ),
                          good.commission == ""
                              ? Container()
                              : Container(
                                  margin: EdgeInsets.only(top: 4, bottom: 4),
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(2.5),
                                      color: HexColor.fromHex(styleModel
                                              .couponCommission
                                              .right
                                              .commissionBgColor ??
                                          ""),
                                      image: DecorationImage(
                                          image: CachedNetworkImageProvider(
                                              styleModel.couponCommission.right
                                                      .commissionBgImg ??
                                                  ""))),
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        left: 8, right: 8, top: 2, bottom: 2),
                                    child: Text(
                                      (styleModel.couponCommission.right
                                                  .commissionText ??
                                              "") +
                                          (good.commission ?? ""),
                                      style: TextStyle(
                                          color: HexColor.fromHex(styleModel
                                              .couponCommission
                                              .right
                                              .commissionFontColor),
                                          fontSize: 22.sp,
                                          fontFamily: 'Din',
                                          package: 'zhiying_base_widget'),
                                    ),
                                  ),
                                ),
                        ],
                      ),

                      /// 价格
                      Padding(
                        padding: const EdgeInsets.only(top: 4, bottom: 4),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.only(bottom: 6.sp),
                              child: Text(
                                "¥",
                                style: TextStyle(
                                    color: HexColor.fromHex(
                                        styleModel.currentPriceColor ?? ""),
                                    fontSize: 20.sp),
                              ),
                            ),
                            Text(
                              good.currentPrice ?? "",
                              style: TextStyle(
                                  color: HexColor.fromHex(
                                      styleModel.currentPriceColor ?? ""),
                                  fontSize: 40.sp,
                                  fontWeight: FontWeight.bold,
                                  fontFamily: 'Din',
                                  package: 'zhiying_base_widget'),
                            ),
                            SizedBox(
                              width: 6,
                            ),
                            Padding(
                              padding: EdgeInsets.only(bottom: 4.sp),
                              child: Text(
                                "¥" + good.marketPrice ?? "",
                                style: TextStyle(
                                    color: HexColor.fromHex(
                                        styleModel.marketPriceColor ?? ""),
                                    fontSize: 22.sp,
                                    decoration: TextDecoration.lineThrough,
                                    fontFamily: 'Din',
                                    package: 'zhiying_base_widget'),
                              ),
                            ),
                          ],
                        ),
                      ),

                      /// 热抢
                      Row(
                        children: <Widget>[
                          Expanded(
                              child: Stack(
                            alignment: Alignment.centerLeft,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Expanded(
                                      child: Container(
                                    alignment: Alignment.centerLeft,
                                    height: 40.w,
                                    padding: EdgeInsets.only(
                                      left: 40.w,
                                    ),
                                    margin:
                                        EdgeInsets.only(right: 20, left: 20.w),
                                    color: HexColor.fromHex(
                                        styleModel.hotRank.bgColor ?? ""),
                                    child: Text(
                                      "热销" + good.inorderCount + "件",
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 22.sp,
                                          fontFamily: 'Din',
                                          package: 'zhiying_base_widget'),
                                    ),
                                  ))
                                ],
                              ),
                              Container(
                                width: 48.w,
                                height: 48.w,
                                child: CachedNetworkImage(
                                  imageUrl:
                                      styleModel?.hotRank?.hotSaleImg ?? "",
                                  width: 48.w,
                                  height: 48.w,
                                  placeholder: (context, _) =>
                                      Container(color: Colors.yellow),
                                  fit: BoxFit.fill,
                                ),
                              ),
                              Align(
                                  alignment: Alignment.centerRight,
                                  child: Container(
                                    height: 48.h,
                                    width: 127.w,
                                    decoration: BoxDecoration(
                                        image: DecorationImage(
                                            image: CachedNetworkImageProvider(
                                                styleModel.hotRank.buyNowImg ??
                                                    ""),
                                            fit: BoxFit.fitWidth),
                                        borderRadius:
                                            BorderRadius.circular(20)),
                                    margin: EdgeInsets.only(right: 0),
                                  ))
                            ],
                          ))
                        ],
                      )
                    ],
                  ),
                ))
              ],
            ),
          ),
          Align(
            alignment: Alignment.topLeft,
            child: Container(
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: CachedNetworkImageProvider(indexImage ?? ""))),
              margin: EdgeInsets.only(left: 40.w, top: 8.h),
              height: 60.w,
              width: 60.w,
            ),
          )
        ],
      ),
    );
  }

  Providers getProvider(String provider) {
    for (var item in styleModel.providers) {
      if (item.type == provider) {
        return item;
      }
    }
  }
}
