assignVariable method

void assignVariable(
  1. AssignVariableAction action,
  2. BuildContext context,
  3. dynamic inputValue,
  4. int loop,
  5. ID? variableID,
)

Implementation

void assignVariable(AssignVariableAction action, BuildContext context,
    dynamic inputValue, int loop, ID? variableID) async {
  final variable = context
      .getVariableById(
        variableID: variableID ?? action.variableID!,
        cubitID: action.cubitID!,
        stateID: action.stateID!,
      )
      .variable;
  switch (action.assingFromType) {
    case AssignVariableActionTypes.valueFromElement:
      {
        if (variable is StringVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(
              data: '$inputValue',
            ),
          );
          return;
        }
        if (variable is StringListVar) {
          final list = (variable.data ?? <String>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            list.add('$inputValue');
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] = '$inputValue';
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            (variable).copyWith(data: list),
          );
          return;
        }
        if (variable is IntVar) {
          if (inputValue is num) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: inputValue.toInt()),
            );
            return;
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is IntListVar) {
          final list = (variable.data ?? <int>[]).whereNotNull().toList();
          if (inputValue is num) {
            if (action.targetIndex == null) {
              list.add(inputValue.toInt());
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = inputValue.toInt();
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is DoubleVar) {
          if (inputValue is num) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: inputValue.toDouble()),
            );
            return;
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is DoubleListVar) {
          final list = (variable.data ?? <double>[]).whereNotNull().toList();
          if (inputValue is num) {
            if (action.targetIndex == null) {
              list.add(inputValue.toDouble());
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = inputValue.toDouble();
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is BoolVar) {
          if (inputValue is bool) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: inputValue),
            );
            return;
          }
          throw Exception('Input value is not a Boolean');
        }
        if (variable is BoolListVar) {
          if (inputValue is bool) {
            final list = (variable.data ?? <bool>[]).whereNotNull().toList();
            if (action.targetIndex == null) {
              list.add(inputValue);
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = inputValue;
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Input value is not a Boolean');
        }
        if (variable is DateTimeVar) {
          context.read<AppLogsCubit>().onNewLog(
                EditorLogEntity(
                  id: _uuid.v1(),
                  content:
                      'Assigning variable DateTime, value: $inputValue, ${inputValue.runtimeType}',
                  createdAt: DateTime.now(),
                ),
              );
          if (inputValue is DateTime) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: inputValue),
            );
            final box = await getBox;
            box.put(variable.id, inputValue.toIso8601String());
            return;
          }
          if (DateTime.tryParse(inputValue) != null) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: DateTime.parse(inputValue)),
            );
            return;
          }
          throw Exception('Input value is not a Boolean');
        }
        if (variable is DateTimeListVar) {
          if (inputValue is DateTime) {
            final list =
                (variable.data ?? <DateTime>[]).whereNotNull().toList();
            if (action.targetIndex == null) {
              list.add(inputValue);
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = inputValue;
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Input value is not a Boolean');
        }
        if (variable is JsonVar) {
          if (action.jsonKey == null) {
            throw Exception('Json key is null');
          }
          _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(
                data: {
                  ...variable.data,
                  action.jsonKey: inputValue,
                },
              ));
          return;
        }
        break;
      }
    case AssignVariableActionTypes.valueFromVariable:
      {
        final temp = (action as AssingVariableFromVariable);
        final assigningVariable = context
            .getVariableById(
              variableID: temp.assigningVariableID,
              cubitID: temp.assigningCubitID,
              stateID: temp.assigningStateID,
            )
            .variable;
        Object? data = Variable.getData(variable);
        if (variable is StringVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: '$data'),
          );
          return;
        }
        if (variable is StringListVar) {
          final list = (variable.data ?? <String>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            if (data == null) return;
            list.add('$data');
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] = '$data';
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            (variable).copyWith(data: list),
          );
          return;
        }
        if (variable is IntVar) {
          if (assigningVariable is IntVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: assigningVariable.data?.toInt()),
            );
            return;
          }
          throw Exception('Assigning variable is not a IntVar');
        }
        if (variable is IntListVar) {
          if (assigningVariable is IntVar) {
            if (assigningVariable.data == null) return;
            final list = (variable.data ?? <int>[]).whereNotNull().toList();
            if (assigningVariable.data is num) {
              if (action.targetIndex == null) {
                list.add(assigningVariable.data!.toInt());
              } else {
                if (action.targetIndex == null) return;
                if (action.targetIndex! >= list.length) return;
                list[action.targetIndex!] = assigningVariable.data!.toInt();
              }
              _updateVariable(
                context,
                action.cubitID!,
                action.stateID!,
                variable.copyWith(data: list),
              );
              return;
            }
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is DoubleVar) {
          if (assigningVariable is DoubleVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: assigningVariable.data),
            );
            return;
          }
          throw Exception('Assigning variable is not a DoubleVar');
        }
        if (variable is DoubleListVar) {
          if (assigningVariable is DoubleVar) {
            if (assigningVariable.data == null) return;
            final list =
                (variable.data ?? <double>[]).whereNotNull().toList();
            if (assigningVariable.data is num) {
              if (action.targetIndex == null) {
                list.add(assigningVariable.data!.toDouble());
              } else {
                if (action.targetIndex == null) return;
                if (action.targetIndex! >= list.length) return;
                list[action.targetIndex!] =
                    assigningVariable.data!.toDouble();
              }
              _updateVariable(
                context,
                action.cubitID!,
                action.stateID!,
                variable.copyWith(data: list),
              );
              return;
            }
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is BoolVar) {
          if (assigningVariable is BoolVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: assigningVariable.data),
            );
            return;
          }
          throw Exception('Assigning variable is not a BoolVar');
        }
        if (variable is BoolListVar) {
          if (assigningVariable is BoolVar) {
            if (assigningVariable.data == null) return;
            final list = (variable.data ?? <bool>[]).whereNotNull().toList();
            if (assigningVariable.data is bool) {
              if (action.targetIndex == null) {
                list.add(assigningVariable.data!);
              } else {
                if (action.targetIndex == null) return;
                if (action.targetIndex! >= list.length) return;
                list[action.targetIndex!] = assigningVariable.data!;
              }
              _updateVariable(
                context,
                action.cubitID!,
                action.stateID!,
                variable.copyWith(data: list),
              );
              return;
            }
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is DateTimeVar) {
          if (assigningVariable is DateTimeVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: assigningVariable.data),
            );
            return;
          }
          throw Exception('Assigning variable is not a DateTimeVar');
        }
        if (variable is DateTimeListVar) {
          if (assigningVariable is DateTimeVar) {
            if (assigningVariable.data == null) return;
            final list =
                (variable.data ?? <DateTime>[]).whereNotNull().toList();
            if (assigningVariable.data is DateTime) {
              if (action.targetIndex == null) {
                list.add(assigningVariable.data!);
              } else {
                if (action.targetIndex == null) return;
                if (action.targetIndex! >= list.length) return;
                list[action.targetIndex!] = assigningVariable.data!;
              }
              _updateVariable(
                context,
                action.cubitID!,
                action.stateID!,
                variable.copyWith(data: list),
              );
              return;
            }
          }
          throw Exception('Input value is not a Number');
        }
        if (variable is JsonVar) {
          if (assigningVariable is JsonVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(
                  data: action.jsonKey == null
                      ? assigningVariable.data
                      : {
                          ...variable.data,
                          action.jsonKey: assigningVariable.data,
                        }),
            );
            return;
          }
          throw Exception('Assigning variable is not a JsonVar');
        }
      }
    case AssignVariableActionTypes.valueFromJson:
      {
        final value = (action as AssingVariableFromJson)
            .assigningJSON
            .getValue(context, loop: loop);
        if (variable is StringVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: '$value'),
          );
          return;
        }
        if (variable is StringListVar) {
          final list = (variable.data ?? <String>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            list.add('$value');
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] = '$value';
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: list),
          );
          return;
        }
        if (value is int) {
          if (variable is IntVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: value),
            );
            return;
          }
          if (variable is IntListVar) {
            final list = (variable.data ?? <int>[]).whereNotNull().toList();
            if (action.targetIndex == null) {
              list.add(value);
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = value;
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Value is a Int, but variable is not.');
        }
        if (value is double) {
          if (variable is DoubleVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: value),
            );
            return;
          }
          if (variable is DoubleListVar) {
            final list =
                (variable.data ?? <double>[]).whereNotNull().toList();
            if (action.targetIndex == null) {
              list.add(value);
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = value;
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Value is a Double, but variable is not.');
        }
        if (value is bool) {
          if (variable is BoolVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: value),
            );
            return;
          }
          if (variable is BoolListVar) {
            final list = (variable.data ?? <bool>[]).whereNotNull().toList();
            if (action.targetIndex == null) {
              list.add(value);
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = value;
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Value is a Bool, but variable is not.');
        }
        if (value is DateTime ||
            (value is String && DateTime.tryParse(value) != null)) {
          if (variable is DateTimeVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: DateTime.parse(value)),
            );
            return;
          }
          if (variable is DateTimeListVar) {
            final list =
                (variable.data ?? <DateTime>[]).whereNotNull().toList();
            if (action.targetIndex == null) {
              list.add(DateTime.parse(value));
            } else {
              if (action.targetIndex == null) return;
              if (action.targetIndex! >= list.length) return;
              list[action.targetIndex!] = DateTime.parse(value);
            }
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(data: list),
            );
            return;
          }
          throw Exception('Value is a DateTime, but variable is not.');
        }
        if (value is Map<String, dynamic> || value is List<dynamic>) {
          if (variable is JsonVar) {
            _updateVariable(
              context,
              action.cubitID!,
              action.stateID!,
              variable.copyWith(
                data: action.jsonKey == null
                    ? value
                    : {
                        ...variable.data,
                        action.jsonKey: value,
                      },
              ),
            );
            return;
          }
          throw Exception('Value is a Json, but variable is not.');
        }
      }
    case AssignVariableActionTypes.valueFromStaticText:
      {
        if (variable is StringVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(
                data: (action as AssingVariableFromStaticTextValue).value),
          );
          final box = await getBox;
          box.put(variable.id, (action).value);
          return;
        }
        if (variable is StringListVar) {
          final list = (variable.data ?? <String>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            list.add((action as AssingVariableFromStaticTextValue).value);
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] =
                (action as AssingVariableFromStaticTextValue).value;
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: list),
          );
          return;
        }
        throw Exception('Variable is not a Text Variable');
      }
    case AssignVariableActionTypes.valueFromStaticInt:
      {
        if (variable is IntVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(
                data: (action as AssingVariableFromStaticIntValue).value),
          );
          return;
        }
        if (variable is IntListVar) {
          final list = (variable.data ?? <int>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            list.add((action as AssingVariableFromStaticIntValue).value);
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] =
                (action as AssingVariableFromStaticIntValue).value;
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: list),
          );
          return;
        }
        throw Exception('Variable is not a Int Variable');
      }
    case AssignVariableActionTypes.valueFromStaticDouble:
      {
        if (variable is DoubleVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(
                data: (action as AssingVariableFromStaticDoubleValue).value),
          );
          return;
        }
        if (variable is DoubleListVar) {
          final list = (variable.data ?? <double>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            list.add((action as AssingVariableFromStaticDoubleValue).value);
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] =
                (action as AssingVariableFromStaticDoubleValue).value;
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: list),
          );
          return;
        }
        throw Exception('Variable is not a Double Variable');
      }
    case AssignVariableActionTypes.valueFromStaticBool:
      {
        if (variable is BoolVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(
                data: (action as AssingVariableFromStaticBoolValue).value),
          );
          return;
        }
        if (variable is BoolListVar) {
          final list = (variable.data ?? <bool>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            list.add((action as AssingVariableFromStaticBoolValue).value);
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] =
                (action as AssingVariableFromStaticBoolValue).value;
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: list),
          );
          return;
        }
        throw Exception('Variable is not a Bool Variable');
      }
    case AssignVariableActionTypes.valueFromStaticDateTime:
      {
        if (variable is DateTimeVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(
                data:
                    (action as AssingVariableFromStaticDateTimeValue).value),
          );
          return;
        }
        if (variable is DateTimeListVar) {
          final list =
              (variable.data ?? <DateTime>[]).whereNotNull().toList();
          if (action.targetIndex == null) {
            list.add((action as AssingVariableFromStaticDateTimeValue).value);
          } else {
            if (action.targetIndex == null) return;
            if (action.targetIndex! >= list.length) return;
            list[action.targetIndex!] =
                (action as AssingVariableFromStaticDateTimeValue).value;
          }
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(data: list),
          );
          return;
        }
        throw Exception('Variable is not a DateTime Variable');
      }
    case AssignVariableActionTypes.valueFromStaticJson:
      {
        if (variable is JsonVar) {
          _updateVariable(
            context,
            action.cubitID!,
            action.stateID!,
            variable.copyWith(
                data: (action as AssingVariableFromStaticJsonValue).value),
          );
          return;
        }
        throw Exception('Variable is not a Json Variable');
      }
  }
}