import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/coupon.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/separator.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class NewCouponWidget extends StatelessWidget {
  final Function(int type) callback;
  // final Function couponDialog;
  final GestureTapCallback callbackEx;
  final Coupon coupon;
  final Function showCouponDetails;
  final int type;

  NewCouponWidget(this.coupon, this.callback,this.callbackEx,this.showCouponDetails, {this.type = 1});

  @override
  Widget build(BuildContext context) {
    return buildCoupon(context);
  }

  String getCouponStoreName(){
    String couponStoreName = coupon.tenantName;
    if(coupon?.storeList != null && coupon.storeId != "0"){
      coupon.storeList.forEach((element) {
        if(element.id == coupon.storeId)
          couponStoreName = element.storeName;
      });
    }
    return couponStoreName;
  }

  ///优惠券ui
  Widget buildCoupon(BuildContext context) {
    // if(coupon != null) {
    //   coupon.isEx = fa;
    // }

    // if (coupon != null) {
    //   print("object: ${coupon.toJson()}");
    // }

    return Container(
      height: (coupon != null && coupon.isEx) ? 163.h : 143.h,
      width: double.infinity,
      margin: EdgeInsets.fromLTRB(14.w, 6.h, 14.w, 6.h),
      padding: EdgeInsets.only(right: 5),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(6),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(12),
            offset: Offset(0, 3),
            blurRadius: 14,
            spreadRadius: 0,
          )
        ],
      ),
        child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 6.w,
            height: double.infinity,
              decoration: BoxDecoration(
                borderRadius: new BorderRadius.only(
                  topLeft: Radius.circular(6),
                  bottomLeft: Radius.circular(6),
                ),
                color: (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3) :Color(0xFF32A060),
              )
          ),
          Expanded(
            flex: 2,
              child:Container(
            margin: EdgeInsets.only(top: 14,left: 14,bottom: 14),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "${coupon != null ? coupon.couponName ?? "" : ""}",
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: 15.sp,
                    height: 1.3.h,
                    fontWeight: MyFontWeight.semi_bold,
                    color: Color(0xFF0D0D0D),
                  ),
                ),
                SizedBox(height:10,),
                Expanded(child:
                (coupon.useStartTime == null &&
                    coupon.useEndTime == null)?
                Text(
                  S.of(context).quantian,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: 12.sp,
                    fontWeight: MyFontWeight.regular,
                    color: (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3) :Color(0xFF4D4D4D),
                  ),
                ):Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "有效期: ",
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 12.sp,
                        fontWeight: MyFontWeight.regular,
                        color: (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3) :Color(0xFF4D4D4D),
                      ),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "${coupon?.useStartTime ??""} ~",
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: MyFontWeight.regular,
                            color: (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3) :Color(0xFF4D4D4D),
                          ),
                        ),
                        SizedBox(height: 5,),
                        Text(
                          "${coupon?.useEndTime ?? ""}",
                          maxLines:1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: MyFontWeight.regular,
                            color: (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3) :Color(0xFF4D4D4D),
                          ),
                        )

                      ],
                    )
                  ],
                ),),
                GestureDetector(
                  onTap: (){
                    showCouponDetails();
                  },
                  child: Row(
                    children: [
                      Text(
                        S.of(context).shiyongxiangqing,
                        style: TextStyle(
                          fontSize: 12.sp,
                          fontWeight: MyFontWeight.regular,
                          color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xFF4D4D4D),
                        ),
                      ),
                      Icon(
                        Icons.keyboard_arrow_right,
                        color: (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3) :Color(0xFF4D4D4D),
                        size: 20,
                      )
                    ],
                  ),
                )
              ],
            ),
          )),
          Expanded(
              flex: 1,
              child:Container(
                margin: EdgeInsets.only(top: 14,left: 12,bottom: 14,),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(child: Container(
                      child: priceWidget(context),
                    )),
                    Container(
                      margin: EdgeInsets.only(right: 15.w),
                      child: rightBtn(context),
                    )
                  ],
                ),
              )),
        ],
      ),
    );
    Container(
      height: (coupon != null && coupon.isEx) ? 168.h : 151.h,
      width: double.infinity,
      margin: EdgeInsets.fromLTRB(14.w, 6.h, 14.w, 6.h),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(12),
            offset: Offset(0, 3),
            blurRadius: 14,
            spreadRadius: 0,
          )
        ],
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Container(
              margin: EdgeInsets.only(top: 14.h, bottom: 8.h),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: Container(
                      child: priceWidget(context),
                    ),
                    flex: 1,
                  ),
                  Expanded(
                    flex: 2,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Container(
                              padding: EdgeInsets.fromLTRB(4, 2, 4, 2),
                              decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.circular(2),
                                border: Border.all(
                                  width: 1,
                                  color: Color(0xFFFF7A1A),
                                  style: BorderStyle.solid,
                                ),
                              ),
                              child: Text(
                                S.of(context).shangjiaquan,
                                style: TextStyle(
                                  fontSize: 10.sp,
                                  fontWeight: MyFontWeight.medium,
                                  color: Color(0xFFFF7A1A),
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 8.w,
                            ),
                            Expanded(
                              child: Text(getCouponStoreName(),
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                  fontSize: 16.sp,
                                  fontWeight: MyFontWeight.semi_bold,
                                  color: Color(0xff353535),
                                ),
                              ),
                              flex: 1,
                            )
                          ],
                        ),
                        Text(
                          coupon != null ? coupon.couponName ?? "" : "",
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Color(0xff727272),
                          ),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: [
                            Visibility(
                              child: Text(
                                // S.of(context).quanchangtongyong,
                                (coupon?.allProduct ?? false) ? S.of(context).quanchangtongyong :"",
                                style: TextStyle(
                                  color: Color(0xFF353535),
                                  fontSize: 12.sp,
                                ),
                              ),
                              visible:
                                  (coupon != null && (coupon.bizType == 1)) ??
                                      false,
                            ),
                            Container(
                              margin: EdgeInsets.only(right: 23.w),
                              child: rightBtn(context),
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            flex: (coupon != null && coupon.isEx) ? 97 : 97,
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 23.w),
            child: MySeparator(
              width: 5.w,
              height: 1.h,
              color: Color(0xFF353535),
            ),
          ),
          Expanded(
            flex: (coupon != null && coupon.isEx) ? 56 : 42,
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 23.w, vertical: 7.h),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        S.of(context).xiangqing,
                        style: TextStyle(
                          color: Color(0xFF353535),
                          fontSize: 10.sp,
                        ),
                      ),
                      GestureDetector(
                        child: Icon(
                          (coupon != null && !coupon.isEx)
                              ? Icons.keyboard_arrow_down
                              : Icons.keyboard_arrow_up,
                          color: Colors.black,
                          size: 18,
                        ), //点击按钮布局
                        onTap: callbackEx,
                      ),
                    ],
                  ),
                  if (coupon != null && coupon.isEx)
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          S.of(context).shiyongriqi,
                          style: TextStyle(
                            color: Color(0xFF353535),
                            fontSize: 10.sp,
                          ),
                        ),
                        Text(
                          (coupon.useStartTime == null &&
                                  coupon.useEndTime == null)
                              ? S.of(context).quantian
                              : "${coupon.useStartTime} - ${coupon.useEndTime}",
                          style: TextStyle(
                            color: Color(0xFF353535),
                            fontSize: 10.sp,
                          ),
                        ),
                      ],
                    ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget priceWidget(BuildContext context) {
    if (coupon.bizType == 1) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.baseline,
            textBaseline: TextBaseline.alphabetic,
            children: [
              Text(
                "¥",
                style: TextStyle(
                  fontSize: 22.sp,
                  fontWeight: MyFontWeight.medium,
                  color: (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                ),
              ),
              SizedBox(
                height: 4.h,
              ),
              Text(
                coupon != null
                    ? double.tryParse("${coupon.discountAmount}" ?? "0")
                    .toInt()
                    .toString()
                    : "",
                style: TextStyle(
                  fontSize: 35.sp,
                  fontWeight: MyFontWeight.semi_bold,
                  color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                ),
              ),
            ],
          ),
          Text(
            S.of(context).manyuankeyong(coupon != null
                ? ("${coupon.fullAmount}" ?? "0")
                    .toString()
                : ""),
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 3) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(bottom:10,top: 7),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  S.of(context).duihuanquan,
                  style: TextStyle(
                    fontSize: 26.sp,
                    fontWeight: MyFontWeight.semi_bold,
                    color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                  ),
                ),
              ],
            ),
          ),
          Text(
            S.of(context).xianshangshiyong,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 5) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(bottom:10,top: 7),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  S.of(context).duihuanquan,
                  style: TextStyle(
                    fontSize: 26.sp,
                    fontWeight: MyFontWeight.semi_bold,
                    color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                  ),
                ),
              ],
            ),
          ),
          Text(
            S.of(context).xianxiashiyong,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 6) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          if(coupon?.limitAmount != "0.00")
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.baseline,
            textBaseline: TextBaseline.alphabetic,
            children: [
              Text(
                "¥",
                style: TextStyle(
                  fontSize: 22.sp,
                  fontWeight: MyFontWeight.medium,
                  color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                ),
              ),
              SizedBox(
                height: 4.h,
              ),
              Text(
                coupon != null
                    ?  "${AppUtils.calculateDouble(double.tryParse(coupon.limitAmount) ?? 0)}"
                    : "",
                style: TextStyle(
                  fontSize: 35.sp,
                  fontWeight: MyFontWeight.semi_bold,
                  color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                ),
              ),
            ],
          ),
        ],
      );
    }else {
      return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // if(coupon.discountPercent != 0)
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.baseline,
            textBaseline: TextBaseline.alphabetic,
            children: [
              Text(
                coupon.discountPercent != 0?( coupon != null
                    ? "${(coupon.discountPercent / 10.0 >= 10) ? 10 : coupon.discountPercent / 10.0}" ??
                    "0"
                    : ""):"",
                style: TextStyle(
                  fontSize: 37.sp,
                  fontWeight: MyFontWeight.semi_bold,
                  color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                ),
              ),
              SizedBox(
                height: 4.h,
              ),
              Text(
                "${coupon.discountPercent != 0 ? "折":""}",
                style: TextStyle(
                  fontSize: 20.sp,
                  fontWeight: MyFontWeight.semi_bold,
                  color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
                ),
              ),
            ],
          ),
          // Text(
          //   S.of(context).quanchangtongyong,
          //   style: TextStyle(
          //     fontSize: 12.sp,
          //     fontWeight: MyFontWeight.semi_bold,
          //     color:  (coupon.status ==2 || coupon.status == 3) ? Color(0xFFB3B3B3):Color(0xff32A060),
          //   ),
          // ),
        ],
      );
    }
  }

  Widget rightBtn(context) {
    if (type == 1) {
      if (coupon != null && coupon.status == 0) {
        return Align(
          alignment: Alignment.centerRight,
          child: InkWell(
            onTap: () {
              callback(type);
            },
            child: Container(
              padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.5),
                color: Color(0xFF32A060),
              ),
              child: Text(
                S.of(context).lingqu,
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: MyFontWeight.medium,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        );
      } else {
        return Align(
          alignment: Alignment.centerRight,
          child: Container(
            padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.5),
              color: Colors.grey,
            ),
            child: Text(
              S.of(context).yilingqu,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: MyFontWeight.medium,
                color: Colors.white,
              ),
            ),
          ),
        );
      }
    } else {
      if (coupon != null && coupon.status == 1) {
        return Align(
          alignment: Alignment.centerRight,
          child: InkWell(
            onTap: () {
              callback(type);
            },
            child: Container(
              width: 64.w,
              height: 19.h,
              decoration: BoxDecoration(
                color: Color(0xFF32A060),
                borderRadius: BorderRadius.circular(10),
              ),
              alignment: Alignment.center,
              child:Text(
                S.of(context).lijishiyong,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: MyFontWeight.regular,
                  color: Color(0xFFFFFFFF),
                ),
              ),
            ),
            // Container(
            //   padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
            //   decoration: BoxDecoration(
            //     borderRadius: BorderRadius.circular(10.5),
            //     color: Color(0xFF32A060),
            //   ),
            //   child: Text(
            //     (coupon.bizType == 5 || coupon.bizType == 3)
            //         ? S.of(context).quhexiao
            //         : S.of(context).qushiyong,
            //     style: TextStyle(
            //       fontSize: 12.sp,
            //       fontWeight: MyFontWeight.medium,
            //       color: Colors.white,
            //     ),
            //   ),
            // ),
          ),
        );
      } else if (coupon != null && coupon.status == 2) {
        return Align(
          alignment: Alignment.centerRight,
          child: Container(
            padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.5),
              // border: Border.all(
              //     width: 1, color: Color(0xFFA0A0A0), style: BorderStyle.solid)
              color: Color(0xFFA0A0A0),
            ),
            child: Text(
              S.of(context).yishiyong,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: MyFontWeight.medium,
                color: Colors.white,
              ),
            ),
          ),
        );
      } else {
        return Align(
          alignment: Alignment.centerRight,
          child: Container(
            padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
            // decoration: BoxDecoration(
            //   borderRadius: BorderRadius.circular(10.5),
            //   // border: Border.all(
            //   //     width: 1, color: Color(0xFFA0A0A0), style: BorderStyle.solid)
            //   color: Color(0xFFA0A0A0),
            // ),
            child: Text(
              S.of(context).yishixiao,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: MyFontWeight.semi_bold,
                color: Color(0xFFB3B3B3),
              ),
            ),
          ),
        );
      }
    }
  }

}