import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/evaluate/model/goods_details_evaluate_model.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_alibc/flutter_alibc.dart';
import 'package:flutter_alibc/alibc_model.dart';
import 'package:provider/provider.dart';

///
/// 商品详情评价Widget
///
class GoodsDetailsEvaluateWidget extends StatelessWidget {
  final Map<String, dynamic> data;
  GoodsDetailsEvaluateModel _model;

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

  UserInfoModel _user;

  /// 点击查看更多
  void _openLookMore(BuildContext context) async {
    if (_user?.token == null || _user.token == '') {
      print('need login...');
      RouterUtil.goLogin(context);
      return;
    }

    // bool isAuth = await TaobaoAuth.isAuth();
    if (!_user.isTBAuth) {
      TaobaoAuth.auth(context);
      return;
    }
    TradeResult result;
    if (!EmptyUtil.isEmpty(_model?.comment_url)) {
      if (Platform.isAndroid) {
        result = await FlutterAlibc.openByUrl(url: _model?.comment_url, backUrl: "alisdk://");
      } else if (Platform.isIOS) {
        result = await FlutterAlibc.openByUrl(url: _model?.comment_url);
      }
      Logger.debug('${result.errorCode}   ${result.errorMessage} ');
    }
  }

  @override
  Widget build(BuildContext context) {
    _user = Provider.of<UserInfoNotifier>(context).userInfo;

    return Visibility(
      visible: !EmptyUtil.isEmpty(_model?.comment_url),
      child: GestureDetector(
        onTap: () => _openLookMore(context),
        behavior: HitTestBehavior.opaque,
        child: Container(
          decoration: BoxDecoration(
            color: HexColor.fromHex(_model?.bgColor),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.topLeftRadius)),
              topRight: Radius.circular(ParseUtil.stringParseDouble(_model?.topRightRadius)),
              bottomLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomLeftRadius)),
              bottomRight: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomRightRadius)),
            )
          ),
          margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(_model?.topMargin), left: ParseUtil.stringParseDouble(_model?.leftRightMargin), right: ParseUtil.stringParseDouble(_model?.leftRightMargin)),
          width: double.infinity,
          padding: const EdgeInsets.only(top: 15, bottom: 15, left: 12.5, right: 12.5),
          child: getMainWidget(_model),
        ),
      ),
    );
  }

  /// 评价以及查看更多
  Widget getMainWidget(GoodsDetailsEvaluateModel model) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        getLeftWidget(model),
        getRightWidget(model),
      ],
    );
  }

  /// 查看更多
  Widget getRightWidget(GoodsDetailsEvaluateModel model) {
    return Row(
      children: <Widget>[
        Text(model?.moreText ?? '查看更多', style: TextStyle(color: HexColor.fromHex(model?.more_color ?? '#999999'), fontSize: 12)),
        const SizedBox(width: 2.5,),
        CachedNetworkImage(imageUrl: model?.moreIcon, width: 12),
      ],
    );
  }

  /// 左边图标加文字
  Widget getLeftWidget(GoodsDetailsEvaluateModel model) {
    return Row(
      children: <Widget>[
        CachedNetworkImage(
          imageUrl: model?.titleIcon ?? '',
          width: 12,
        ),
        const SizedBox(width: 7),
        Text(model?.title ?? '宝贝评价', style: TextStyle(color: HexColor.fromHex(model?.titleColor ?? '#333333'), fontSize: 12)),
      ],
    );
  }
}
