import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/title/model/goods_details_title_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

///
/// 商品详情标题
///
class GoodsDetailsTitleWidget extends StatelessWidget {
  final Map<String, dynamic> model;
  GoodsDetailsTitleModel _model;

  GoodsDetailsTitleWidget(this.model, {Key key}) : super(key: key) {
    try {
      _model = GoodsDetailsTitleModel.fromJson(jsonDecode(model['data']));
    } catch (e) {
      Logger.log('GoodsDetailsTitleWidget e = $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 6, bottom: 6),
      margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(_model?.topMargin), left: ParseUtil.stringParseDouble(_model?.leftRightMargin), right: ParseUtil.stringParseDouble(_model?.leftRightMargin)),
      decoration: BoxDecoration(
        color: HexColor.fromHex(_model?.bgColor),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.topLeftRadius)),
          topRight: Radius.circular(ParseUtil.stringParseDouble(_model?.topRightRadius)),
          bottomRight: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomRightRadius)),
          bottomLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomLeftRadius)),
        )
      ),
      child: _getMaiWidget(_model),
    );
  }

  /// 主widget
  Widget _getMaiWidget(GoodsDetailsTitleModel model) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        _buildPriceWidget(model),
        const SizedBox(height: 6.5),
        _buildTitleWidget(model)
      ],
    );
  }

  /// 价格
  Widget _buildPriceWidget(GoodsDetailsTitleModel model) {
    return  Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        _getLeftWidget(model),

        /// 右边widget
        _getBuyNumberWidget(model),
      ],
    );
  }

  /// 左边的wiget 包括价格等
  Widget _getLeftWidget(GoodsDetailsTitleModel model) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        /// 价格
        _getPriceWidget(model),
        const SizedBox(width: 5),

        /// 券后
        _getQhPriceWidget(model),
        const SizedBox(width: 5),

        /// 积分
        Visibility(
            visible: !EmptyUtil.isEmpty(model?.point),
            child: _getPointsWidget(model))
      ],
    );
  }

  /// 价格
  Widget _getPriceWidget(GoodsDetailsTitleModel model) {
    return Row(
      children: <Widget>[
        Text('¥ ', style: TextStyle(color: HexColor.fromHex( model?.newPriceColor ??'#FF4242'), fontSize: 15)),
        Text(model?.currentPrice ?? '0', style: TextStyle(color: HexColor.fromHex(model?.newPriceColor ?? '#FF4242'), fontSize: 30, fontFamily: 'Din', package: 'zhiying_comm', fontWeight: FontWeight.bold)),
      ],
    );
  }

  /// 积分
  Widget _getPointsWidget(GoodsDetailsTitleModel model) {
    return Container(
      decoration: BoxDecoration(color: HexColor.fromHex(model?.rewardBgColor ?? '#FFEFDA'), borderRadius: BorderRadius.circular(5)),
      padding: const EdgeInsets.only(left: 4, right: 7.5, top: 4, bottom: 4),
      child: Text(model?.point ?? '+ 0.00 积分', style: TextStyle(color: HexColor.fromHex( model?.rewardTextColor ?? '#B78107'), fontSize: 9, fontFamily: 'Din', package: 'zhiying_comm')),
    );
  }

  /// 券后价格
  Widget _getQhPriceWidget(GoodsDetailsTitleModel model) {
    return Column(
      children: <Widget>[
        Text( model?.isCoupon == '1' ?  model?.couponText ?? '券后' : /*model?.hand_price_text ??*/ '到手价', style: TextStyle(color: HexColor.fromHex(model?.couponTextColor ?? '#FF4242'), fontSize: 11)),
        Text('¥ ${model?.marketPrice}' ?? '¥ 0', style: TextStyle(color: HexColor.fromHex(model?.saleTextColor ?? '#999999'), fontSize: 10, fontFamily: 'Din', package: 'zhiying_comm', decoration: TextDecoration.lineThrough)),
      ],
    );
  }

  /// 购买人数
  Widget _getBuyNumberWidget(GoodsDetailsTitleModel model) {
    // 考拉和唯品会不展示
    if(model?.provider == GlobalConfig.PROVIDER_VIP || model?.provider == GlobalConfig.PROVIDER_KL){
      return Container();
    }
    return Text('${model?.soldCount?? 0}${model?.goBuyText ?? '人已购买'}' ?? '99999人已购买', style: TextStyle(color: HexColor.fromHex(model?.goBuyColor ?? '#999999'), fontSize: 12.5));
  }


  /// 标题
  Widget _buildTitleWidget(GoodsDetailsTitleModel model) {
    return RichText(
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(children: [
        WidgetSpan(child: _getGoodsTypeIcon(model)),
        _getGoodsTitle(model),
      ]),
    );
  }

  /// 商品类型图标
  Widget _getGoodsTypeIcon(GoodsDetailsTitleModel model) {
    return Container(
      // height: 15,
      // width: 32,
      padding: const EdgeInsets.only(left: 7.5, right: 7.5, bottom: 2, top: 3),
      margin: const EdgeInsets.only(right: 4),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(2.5), color: HexColor.fromHex(getPlatformCssByPvd()?.bgColor)),
      child: Text(getPlatformCssByPvd()?.name ?? '', textAlign: TextAlign.center, style: TextStyle(color: HexColor.fromHex(getPlatformCssByPvd()?.textColor ?? ''), fontSize: 9)),
    );
  }

  /// 商品标题
  InlineSpan _getGoodsTitle(GoodsDetailsTitleModel model) {
    return TextSpan(
        text: model?.title ?? '',
        style: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 14,
          color: HexColor.fromHex(model?.titleTextColor ?? '#FF333333'),
        ));
  }

  PlatformCss getPlatformCssByPvd() {
    PlatformCss rlt = null;
    this._model.platformCss.forEach((element) {
      if(element.type == this._model.provider) {
        rlt = element;
      }
    });
    return rlt;
  }

}
