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(12.5),
            margin:
                EdgeInsets.only(top: 4, bottom: 4, left: 12.5, right: 12.5),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(12.5)),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                /// 商品图片
                Container(
                  width:127,
                  height: 127,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(6),
                    child: CachedNetworkImage(
                      imageUrl: good?.goodImage ?? '',
                    ),
                  ),
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(6)),
                ),

                /// 商品图片右边视图
                Expanded(
                    child: Container(
                  margin: EdgeInsets.only(left: 10),
                  child: Column(
                    // mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    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: 2, right: 2, 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: 9),
                                  ),
                                )),
                            WidgetSpan(
                                child: SizedBox(
                              width: 2,
                            )),
                            TextSpan(
                                text: good.goodTitle,
                                style: TextStyle(
                                    color: HexColor.fromHex(
                                        styleModel.titleColor ?? ""),
                                    fontSize: 15))
                          ])),

                      /// 优惠券
                      Row(
                        children: <Widget>[
                          good.coupon == ""
                              ? Container()
                              : Container(
                                  margin: EdgeInsets.only(
                                      top: 4, bottom: 4, right: 7.5),
                                  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: 11,
                                          fontFamily: 'Din',
                                          package: 'zhiying_comm'),
                                    ),
                                  ),
                                ),
                          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: 11,
                                          fontFamily: 'Din',
                                          package: 'zhiying_comm'),
                                    ),
                                  ),
                                ),
                        ],
                      ),

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

                      /// 热抢
                      Row(
                        children: <Widget>[
                          Expanded(
                              child: Stack(
                            alignment: Alignment.centerLeft,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Expanded(
                                      child: Container(
                                    alignment: Alignment.centerLeft,
                                    height: 20,
                                    padding: EdgeInsets.only(
                                      left: 20,
                                    ),
                                    margin:
                                        EdgeInsets.only(right: 20, left: 10),
                                    color: HexColor.fromHex(
                                        styleModel.hotRank.bgColor ?? ""),
                                    child: Text(
                                      "热销" + (good.inorderCount??"") + "件",
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 11,
                                          fontFamily: 'Din',
                                          package: 'zhiying_comm'),
                                    ),
                                  ))
                                ],
                              ),
                              Container(
                                width: 24,
                                height: 24,
                                child: CachedNetworkImage(
                                  imageUrl:
                                      styleModel?.hotRank?.hotSaleImg ?? "",
                                  width: 24,
                                  height: 24,
                                  placeholder: (context, _) =>
                                      Container(color: Colors.yellow),
                                  fit: BoxFit.fill,
                                ),
                              ),
                              Align(
                                  alignment: Alignment.centerRight,
                                  child: Container(
                                    height: 24,
                                    width: 63.5,
                                    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: 20, top: 4),
              height: 30,
              width: 30,
            ),
          )
        ],
      ),
    );
  }

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