import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_model.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

// ignore: must_be_immutable
class HomeGoodsItem extends StatelessWidget {
  final HomeGoodsModel goods;
  final HomeGoodsStyleModel style;
  Map<String, dynamic> data;

  HomeGoodsItem(this.goods, this.style, {Key key, this.data})
      : super(key: key) {
    if (this.data != null && this.data.containsKey('data')) {
      String data = this.data['data'];
      Map<String, dynamic> json = Map<String, dynamic>.from(jsonDecode(data));
      this.data = json;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 12.5, right: 12.5, top: 4, bottom: 4),
      padding: EdgeInsets.all(7.5),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(7.5))),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            width: 126,
            height: 126,
            margin: EdgeInsets.only(right: 10),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(7.5),
              child: CachedNetworkImage(
                imageUrl: goods.goodImage,
                fit: BoxFit.fitHeight,
              ),
            ),
          ),
          Expanded(
            child: Container(
              height: 126,
              width: double.infinity,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    width: double.infinity,
                    child: _createTitle(),
                  ),
                  _createShop(),
                  _createCupone(),
                  Expanded(child: Container()),
                  _createBottom(),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _createTitle() {
    List<InlineSpan> list = List();
    if (goods.providerName != null && goods.providerName != '') {
      list.add(WidgetSpan(
        child: Container(
          padding: EdgeInsets.only(left: 2, right: 2, top: 3, bottom: 3),
          margin: EdgeInsets.only(right: 4),
          child: Text(
            goods.providerName,
            style: TextStyle(
              fontSize: 9,
              height: 1,
              color: HexColor.fromHex(style.providerNameColor),
            ),
          ),
          decoration: BoxDecoration(
              color: HexColor.fromHex(style.providerNameBackgroundColor),
              borderRadius: BorderRadius.circular(2.5)),
        ),
      ));
    }
    list.add(TextSpan(
      text: goods.goodTitle,
      style: TextStyle(
          fontSize: 15,
          color: HexColor.fromHex('#333333'),
          fontWeight: FontWeight.bold),
    ));
    return RichText(
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(children: list),
    );
  }

  Widget _createShop() {
    return Container(
      margin: EdgeInsets.only(top: 5),
      width: double.infinity,
      child: Row(
        children: <Widget>[
          Container(
            width: 15,
            height: 15,
            margin: EdgeInsets.only(right: 2),
            child: CachedNetworkImage(
              imageUrl: style.shopIcon,
              fit: BoxFit.fitHeight,
            ),
          ),
          Expanded(
              child: Text(
            goods.shopName,
            style: TextStyle(
                color: HexColor.fromHex(style.shopNameColor),
                fontSize: 11,
                fontWeight: FontWeight.w400),
          ))
        ],
      ),
    );
  }

  Widget _createCupone() {
    List<Widget> widgets = List();
    if (goods.coupon != null && goods.coupon != '') {
      widgets.add(Container(
        margin: EdgeInsets.only(right: 5),
        padding: EdgeInsets.only(left: 10, right: 10, top: 3, bottom: 3),
        decoration: BoxDecoration(
          color: HexColor.fromHex(style.couponBackgroundColor),
          borderRadius: BorderRadius.circular(2.5),
        ),
        child: Text(
          goods.coupon,
          textAlign: TextAlign.center,
          maxLines: 1,
          style: TextStyle(
            height: 1,
            fontSize: 11,
            color: HexColor.fromHex(style.couponFontColor),
          ),
        ),
      ));
    }

    if (goods.commission != null || goods.commission != '') {
      widgets.add(Container(
        margin: EdgeInsets.only(right: 5),
        padding: EdgeInsets.only(left: 10, right: 10, top: 3, bottom: 3),
        decoration: BoxDecoration(
          color: HexColor.fromHex(style.commissionBackgroundColor),
          borderRadius: BorderRadius.circular(2.5),
        ),
        child: Text(
          goods.commission,
          textAlign: TextAlign.center,
          maxLines: 1,
          style: TextStyle(
            height: 1,
            fontSize: 11,
            color: HexColor.fromHex(style.commissionFontColor),
          ),
        ),
      ));
    }

    return Container(
      margin: EdgeInsets.only(top: 6),
      child: Row(
        children: widgets,
      ),
    );
  }

  Widget _createBottom() {
    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          _createPrice(),
          _createOriginPrice(),
          Expanded(child: Container()),
          _createSale(),
        ],
      ),
    );
  }

  Widget _createPrice() {
    return RichText(
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(children: [
        TextSpan(
          text: '￥',
          style: TextStyle(
            fontSize: 10,
            color: HexColor.fromHex(style.currentPriceColor),
            fontWeight: FontWeight.bold,
            fontFamily: 'Din',
            package: 'zhiying_base_widget',
          ),
        ),
        TextSpan(
          text: goods.currentPrice,
          style: TextStyle(
            fontSize: 20,
            color: HexColor.fromHex(style.currentPriceColor),
            fontWeight: FontWeight.bold,
            fontFamily: 'Din',
            package: 'zhiying_base_widget',
          ),
        )
      ]),
    );
  }

  Widget _createOriginPrice() {
    return Padding(
        padding: EdgeInsets.only(bottom: 2),
        child: Text(
          '￥${goods.marketPrice}',
          style: TextStyle(
            fontSize: 11,
            height: 1,
            color: HexColor.fromHex(style.marketPriceColor),
            fontWeight: FontWeight.normal,
            fontFamily: 'Din',
            package: 'zhiying_base_widget',
            decoration: TextDecoration.lineThrough,
          ),
        ));
  }

  Widget _createSale() {
    return Text(
      '${goods.inorderCount}',
      style: TextStyle(fontSize: 11, color: HexColor.fromHex('#999999')),
    );
  }
}
