import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/sign_info.dart';
import 'package:huixiang/retrofit/data/task.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/view_widget/round_button.dart';

class IntegralTask extends StatefulWidget {
  final SignInfo signInfo;

  IntegralTask(this.signInfo);

  @override
  State<StatefulWidget> createState() {
    return _IntegralTask();
  }
}

class _IntegralTask extends State<IntegralTask> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width - 32,
      height: 300.h,
      margin: EdgeInsets.all(16),
      padding: EdgeInsets.symmetric(
        horizontal: 10.w,
        vertical: 20.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.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 10.w),
            child: Text(
              S.of(context).zuorenwudejifen,
              style: TextStyle(
                color: Color(0xFF353535),
                fontWeight: MyFontWeight.medium,
                fontSize: 16.sp,
              ),
            ),
          ),
          Expanded(
            child: Container(
              child: Swiper(
                  loop: false,
                  physics: BouncingScrollPhysics(),
                  pagination: SwiperPagination(
                    alignment: Alignment.bottomCenter,
                    builder: DotSwiperPaginationBuilder(
                      size: 8,
                      activeSize: 8,
                      space: 5,
                      activeColor: Colors.black,
                      color: Colors.black.withAlpha(76),
                    ),
                  ),
                  itemBuilder: (context, position) {
                    return taskPage(position);
                  },
                  itemCount: (widget.signInfo != null &&
                          widget.signInfo.taskList != null &&
                          widget.signInfo.taskList.length > 0)
                      ? (widget.signInfo.taskList.length < 3
                          ? 1
                          : (widget.signInfo.taskList.length ~/ 3 +
                              (widget.signInfo.taskList.length % 3 > 0
                                  ? 1
                                  : 0)))
                      : 1),
            ),
            flex: 1,
          )
        ],
      ),
    );
  }

  Widget taskPage(position) {
    if (widget.signInfo == null || widget.signInfo.taskList == null)
      return Container();
    return Container(
      margin: EdgeInsets.only(left: 10.w, right: 10.w, top: 16.h),
      child: Column(
        children: [
          tashItem(widget.signInfo.taskList[position * 3 + 0]),
          if (widget.signInfo.taskList.length > (position * 3 + 1))
            tashItem(widget.signInfo.taskList[position * 3 + 1]),
          if (widget.signInfo.taskList.length > (position * 3 + 2))
            tashItem(widget.signInfo.taskList[position * 3 + 2]),
        ],
      ),
    );
  }

  taskImg(String taskType) {
    switch (taskType) {
      case "bill_type_point_login":
        return "assets/image/icon_integral_share.png";
        break;
      case "bill_type_point_order":
        return "assets/image/icon_integral_order.png";
        break;
      case "bill_type_point_signin":
        return "assets/image/icon_integral_sign.png";
        break;
    }
    return "assets/image/icon_integral_share.png";
  }

  Widget tashItem(Task task) {
    return Container(
      margin: EdgeInsets.only(top: 8.h, bottom: 8.h),
      alignment: Alignment.center,
      child: Row(
        children: [
          Image.asset(
            taskImg(task.type),
            width: 24.w,
            height: 24.h,
          ),
          SizedBox(
            width: 21.w,
          ),
          Expanded(
            flex: 1,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  task.name,
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: MyFontWeight.regular,
                    color: Color(0xFF353535),
                  ),
                ),
                SizedBox(
                  height: 7.h,
                ),
                Row(
                  children: [
                    Text(
                      "+${double.tryParse(task.rewardValue).toInt()}",
                      style: TextStyle(
                        fontSize: 12.sp,
                        fontWeight: MyFontWeight.regular,
                        color: Color(0xFF727272),
                      ),
                    ),
                    SizedBox(
                      width: 20.w,
                    ),
                    Text(
                      S.of(context).wancheng_(
                          "${task.complateNum ?? task.conplateNum}/${task.limitDay}"),
                      style: TextStyle(
                        fontSize: 12.sp,
                        fontWeight: MyFontWeight.regular,
                        color: Color(0xFF727272),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          RoundButton(
            text: task.limitDay == (task.complateNum ?? task.conplateNum)
                ? S.of(context).yiwancheng
                : S.of(context).quwancheng,
            textColor: Colors.white,
            backgroup: task.limitDay == (task.complateNum ?? task.conplateNum)
                ? Color(0xFFA0A0A0)
                : Color(0xFF32A060),
            radius: 12.w,
            fontSize: 14.sp,
            fontWeight: FontWeight.bold,
            padding: EdgeInsets.symmetric(vertical: 4.h, horizontal: 12.w),
          ),
        ],
      ),
    );
  }
}