import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:keyboard/utils/grid_util.dart';
import 'package:keyboard/utils/upload/upload_scene_type.dart';

import '../data/bean/widget_location.dart';
import '../data/consts/constants.dart';
import '../module/intimacy_analyse/intimacy_analyse_upload/widget/upload_nine_grid.dart';
import '../resource/assets.gen.dart';

/// 步骤卡片工具类
class StepCardUtil {
  StepCardUtil._();

  /// 是否有添加按钮，如果图片数量小于最大数量，则总数量，要添加1个添加图片的条目
  static bool isNeedAddItem(Mode mode, int itemCount, int maxCount) {
    // 预览模式，不需要添加
    if (mode == Mode.preview) {
      return false;
    }
    // 编辑模式，才有添加按钮
    return itemCount < maxCount;
  }

  /// 计算实际的itemCount
  static getRealItemCount(Mode mode, int itemCount, int maxCount) {
    int realItemCount;
    // 如果图片数量小于最大数量，则总数量，要添加1个添加图片的条目
    if (isNeedAddItem(mode, itemCount, maxCount)) {
      realItemCount = itemCount + 1;
    } else {
      // 满了最大数量，则直接显示所有图片
      realItemCount = itemCount;
    }
    return realItemCount;
  }

  /// 获取卡片背景图片
  static ImageProvider getCardBgImageProvider({
    required int itemCount,
    required UploadSceneType sceneType,
    required Mode mode,
    int maxCount = Constants.uploadNineGridMaxCount,
    int crossAxisCount = Constants.uploadNineGridCrossAxisCount,
  }) {
    int realItemCount = getRealItemCount(mode, itemCount, maxCount);
    // 亲密度分析、对话分析
    if (UploadSceneType.intimacyAnalyse == sceneType ||
        UploadSceneType.conversationAnalysis == sceneType) {
      // 计算出九宫格有多少行
      int rowCount = GridUtil.calculateRowCount(realItemCount, crossAxisCount);
      // 根据行数不同，使用不同的步骤卡片背景
      if (rowCount == 1) {
        return Assets.images.bgIntimacyAnalyseUploadCard.provider();
      } else if (rowCount == 2) {
        return Assets.images.bgIntimacyAnalyseUploadCard2.provider();
      } else {
        return Assets.images.bgIntimacyAnalyseUploadCard3.provider();
      }
    } else {
      // 识图回复
      return Assets.images.bgIntimacyAnalyseUploadCard2.provider();
    }
  }

  /// 计算顶部Icon的位置
  static WidgetLocation calculateTopIconWidgetLocation({
    required int itemCount,
    required UploadSceneType sceneType,
    required Mode mode,
    int maxCount = Constants.uploadNineGridMaxCount,
    int crossAxisCount = Constants.uploadNineGridCrossAxisCount,
  }) {
    // 识图回复
    if (UploadSceneType.scanImageReply == sceneType) {
      return WidgetLocation(null, -8.h, 0, null);
    } else {
      int realItemCount = getRealItemCount(mode, itemCount, maxCount);
      // 计算出九宫格有多少行
      int rowCount = GridUtil.calculateRowCount(realItemCount, crossAxisCount);
      // 亲密度分析
      if (UploadSceneType.intimacyAnalyse == sceneType) {
        if (rowCount == 1) {
          return WidgetLocation(null, -9.h, 0, null);
        } else if (rowCount == 2) {
          return WidgetLocation(null, -11.h, 0, null);
        } else {
          return WidgetLocation(null, -11.h, 0, null);
        }
      } else {
        // 对话分析
        if (rowCount == 1) {
          return WidgetLocation(null, -14.h, 0, null);
        } else if (rowCount == 2) {
          return WidgetLocation(null, -13.h, 0, null);
        } else {
          return WidgetLocation(null, -13.h, 0, null);
        }
      }
    }
  }
}
