import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/price/model/goods_details_price_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

///
/// 商品详情价格widget
///
class GoodsDetailsPriceWidget extends StatelessWidget {
  final Map<String, dynamic> model;
  GoodsDetailsPriceModel _model;
  GoodsDetailsPriceWidget(this.model, {Key key}) : super(key: key){
    try{
      _model = GoodsDetailsPriceModel.fromJson(jsonDecode(model['data']));
    }catch(e){
     Logger.log('GoodsDetailsPriceWidget  e = $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Colors.white,
      padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 7.5),
      child: _getMainWidget(_model),
    );
  }

  /// 主体视图
  Widget _getMainWidget(GoodsDetailsPriceModel model) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        _getLeftWidget(model),

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

  /// 左边的wiget 包括价格等
  Widget _getLeftWidget(GoodsDetailsPriceModel 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(GoodsDetailsPriceModel model) {
    return Row(
      children: <Widget>[
        Text(model?.symbol ?? '¥ ', style: TextStyle(color: HexColor.fromHex( model?.price_color ??'#FF4242'), fontSize: 15)),
        Text(model?.current_price ?? '0', style: TextStyle(color: HexColor.fromHex(model?.price_color ?? '#FF4242'), fontSize: 30, fontFamily: 'Din', package: 'zhiying_base_widget', fontWeight: FontWeight.bold)),
      ],
    );
  }

  /// 积分
  Widget _getPointsWidget(GoodsDetailsPriceModel model) {
    return Container(
      decoration: BoxDecoration(color: HexColor.fromHex(model?.points_bg_color ?? '#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?.points_color ?? '#B78107'), fontSize: 9, fontFamily: 'Din', package: 'zhiying_base_widget')),
    );
  }

  /// 券后价格
  Widget _getQhPriceWidget(GoodsDetailsPriceModel model) {
    return Column(
      children: <Widget>[
        Text(model?.ticket ?? '券后', style: TextStyle(color: HexColor.fromHex(model?.ticket_color ?? '#FF4242'), fontSize: 11)),
        Text('${model?.symbol}${model?.market_price}' ?? '¥ 199', style: TextStyle(color: HexColor.fromHex(model?.ticket_price_color ?? '#FF4242'), fontSize: 10, fontFamily: 'Din', package: 'zhiying_base_widget', decoration: TextDecoration.lineThrough)),
      ],
    );
  }

  /// 购买人数
  Widget _getBuyNumberWidget(GoodsDetailsPriceModel model) {
    return Text('${model?.sold_count}${model?.buy_text}' ?? '99999人已购买', style: TextStyle(color: HexColor.fromHex(model?.buy_color ?? '#999999'), fontSize: 12.5));
  }
}
