import 'dart:io';

import 'callbacks.dart';
import 'display_image_source.dart';

class MultiImgController {
  List<String> imageUrls = [];
  List<File> imageFiles = [];
  List<DisplayImageSource> addedFiles = [];
  List<DisplayImageSource> removedFiles = [];

  List<DisplayImageSource> fileContainers = [];
  CallBack callback;
  MultiImgController() {
    fileContainers = [];
  }

  set setImageUrls(List<String> imageUrls) {
    if (imageUrls == null) {
      return;
    }
    fileContainers.clear();

    this.imageUrls = imageUrls;
    imageUrls.forEach((e) {
      fileContainers.add(DisplayImageSource(url: e));
    });
    if (callback != null) {
      callback();
    }
  }

  set setImageFiles(List<File> imageFiles) {
    if (imageFiles == null) {
      return;
    }

    fileContainers.clear();

    this.imageFiles = imageFiles;
    imageFiles.forEach((e) {
      fileContainers.add(DisplayImageSource(file: e));
    });
    if (callback != null) {
      callback();
    }
  }

  void onChange(CallBack callBack) {
    this.callback = callBack;
  }

  set addFile(DisplayImageSource fileContainer) {
    // if (fileContainers.contains(fileContainer)) return;
    addedFiles.add(fileContainer);
    if (callback != null) {
      callback();
    }
  }

  set removeFile(DisplayImageSource fileContainer) {
    if (!fileContainers.contains(fileContainer)) return;
    fileContainers.remove(fileContainer);

    if (addedFiles.contains(fileContainer)) {
      addedFiles.remove(fileContainer);
    }
    if (imageUrls.contains(fileContainer.url)) {
      removedFiles.add(fileContainer);
    }

    if (imageFiles.contains(fileContainer.file)) {
      this.imageFiles.remove(fileContainer.file);
    }

    if (callback != null) {
      callback();
    }
  }

  List<File> get getUpdatedFile {
    List<File> _addfiles = getAddedFile;
    this.imageFiles.addAll(_addfiles);
    return this.imageFiles;
  }

  List<File> get getAddedFile {
    return addedFiles.map((e) => e.file).toList();
  }

  List<String> get getDeletedUrl {
    return removedFiles.map((e) => e.url).toList();
  }
}
