package com.reactnativezoom.videosdk;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.reactnativezoom.videosdk.convert.RNZoomVideoSDKErrors;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;

import us.zoom.sdk.ZoomVideoSDK;
import us.zoom.sdk.ZoomVideoSDKVideoView;
import us.zoom.sdk.ZoomVideoSDKVirtualBackgroundHelper;
import us.zoom.sdk.ZoomVideoSDKVirtualBackgroundItem;

public class RNZoomVideoSdkVirtualBackgroundHelperModule extends ReactContextBaseJavaModule {

  private final ReactApplicationContext reactContext;
  private ZoomVideoSDKVideoView videoView;

  RNZoomVideoSdkVirtualBackgroundHelperModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
  }

  @Override
  public String getName() {
    return "RNZoomVideoSdkVirtualBackgroundHelper";
  }

  private ZoomVideoSDKVirtualBackgroundHelper getVirtualBackgroundHelper() {
    ZoomVideoSDKVirtualBackgroundHelper virtualBackgroundHelper = null;
    try {
      virtualBackgroundHelper = ZoomVideoSDK.getInstance().getVirtualBackgroundHelper();
      if (virtualBackgroundHelper == null) {
        throw new Exception("No Virtual Background Helper Found");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return virtualBackgroundHelper;
  }

  @ReactMethod
  public void isSupportVirtualBackground(Promise promise) {
    if (Utils.checkRNActivity(reactContext, promise)) {
      return;
    }
    reactContext.getCurrentActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        promise.resolve(getVirtualBackgroundHelper().isSupportVirtualBackground());
      }
    });
  }

  @ReactMethod
  public void addVirtualBackgroundItem(String filePath, Promise promise) {
    if (Utils.checkRNActivity(reactContext, promise)) {
      return;
    }
    reactContext.getCurrentActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Bitmap bitmap = null;
        InputStream inputStream = null;
        try {
          inputStream = reactContext.getContentResolver().openInputStream(Uri.parse(filePath));
          bitmap = BitmapFactory.decodeStream(inputStream);
          ZoomVideoSDKVirtualBackgroundItem item = getVirtualBackgroundHelper().addVirtualBackgroundItem(bitmap);
          promise.resolve(RNZoomVideoSdkVirtualBackgroundItemModule.mapVBItem(item));
        } catch (FileNotFoundException e) {
          promise.reject("FileNotFoundException", "File does not exist", (Throwable) null);
          e.printStackTrace();
        }
      }
    });
  }

  @ReactMethod
  public void removeVirtualBackgroundItem(String imageName, Promise promise) {
    if (Utils.checkRNActivity(reactContext, promise)) {
      return;
    }
    reactContext.getCurrentActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        int ret = 1;
        List<ZoomVideoSDKVirtualBackgroundItem> itemList = getVirtualBackgroundHelper().getVirtualBackgroundItemList();
        if (itemList != null && itemList.size() > 0) {
          for (ZoomVideoSDKVirtualBackgroundItem item : itemList) {
            if(item.getImageName().equals(imageName)) {
              ret = getVirtualBackgroundHelper().removeVirtualBackgroundItem(item);
            }
          }
          promise.resolve(RNZoomVideoSDKErrors.valueOf(ret));
        } else {
          promise.reject("removeVirtualBackgroundItem_fail", "No virtual background item found", (Throwable) null);
        }
      }
    });
  }

  @ReactMethod
  public void getVirtualBackgroundItemList(Promise promise) {
    if (Utils.checkRNActivity(reactContext, promise)) {
      return;
    }
    reactContext.getCurrentActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        List<ZoomVideoSDKVirtualBackgroundItem> itemList = getVirtualBackgroundHelper().getVirtualBackgroundItemList();
        if (itemList == null) {
          itemList = new ArrayList<>();
        }
        promise.resolve(RNZoomVideoSdkVirtualBackgroundItemModule.mapItemArray(itemList));
      }
    });
  }

  @ReactMethod
  public void setVirtualBackgroundItem(String imageName, Promise promise) {
    if (Utils.checkRNActivity(reactContext, promise)) {
      return;
    }
    reactContext.getCurrentActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        int ret = 1;
        List<ZoomVideoSDKVirtualBackgroundItem> itemList = getVirtualBackgroundHelper().getVirtualBackgroundItemList();
        if (itemList != null && itemList.size() > 0) {
          for (ZoomVideoSDKVirtualBackgroundItem item : itemList) {
            if(item.getImageName().equals(imageName)) {
              ret = getVirtualBackgroundHelper().setVirtualBackgroundItem(item);
            }
          }
          promise.resolve(RNZoomVideoSDKErrors.valueOf(ret));
        } else {
          promise.reject("setVirtualBackgroundItem_fail", "No virtual background item found", (Throwable) null);
        }
      }
    });
  }

  @ReactMethod
  public void getSelectedVirtualBackgroundItem(Promise promise) {
    if (Utils.checkRNActivity(reactContext, promise)) {
      return;
    }
    reactContext.getCurrentActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        ZoomVideoSDKVirtualBackgroundItem item = getVirtualBackgroundHelper().getSelectedVirtualBackgroundItem();
        promise.resolve(RNZoomVideoSdkVirtualBackgroundItemModule.mapVBItem(item));
      }
    });
  }
}
