package com.lura.rn;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;

import com.anvato.androidsdk.integration.AnvatoConfig;
import com.anvato.androidsdk.integration.AnvatoGlobals;
import com.anvato.androidsdk.integration.AnvatoPlaybackOptions;
import com.anvato.androidsdk.integration.AnvatoSDK;
import com.anvato.androidsdk.integration.AnvatoSDKException;
import com.anvato.androidsdk.player.AnvatoPlayerUI;
import com.facebook.react.uimanager.ThemedReactContext;

import org.json.JSONException;
import org.json.JSONObject;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Objects;

@SuppressLint("ViewConstructor")
public class LuraPlayerNativeView extends LinearLayout implements AnvatoGlobals.AnvatoVideoEventListener, AnvatoGlobals.AnvatoDataEventListener, AnvatoConfig.AnvatoConfigCallback {
  private final ThemedReactContext context;
  private AnvatoSDK anvatoSDK;
  private final AnvatoPlayerUI anvatoPlayerUI;

  private String accessKey;
  private String video;

  private String token;

  private boolean sdkReady;

  public LuraPlayerNativeView(ThemedReactContext context) {
    super(context);
    this.context = context;
    this.anvatoPlayerUI = new AnvatoPlayerUI(context.getCurrentActivity());
    this.video = null;
    this.sdkReady = false;
    this.addView(this.anvatoPlayerUI);
  }

  @RequiresApi(api = Build.VERSION_CODES.O)
  public void setToken(String token) {
    String[] arr = token.split("\\.");
    if (arr.length == 3) {
      this.token = token;
      String body = arr[1];
      try {
        JSONObject json = new JSONObject(
          new String(Base64.getDecoder().decode(body.getBytes(StandardCharsets.UTF_8)))
        );
        String accessKey = json.getString("iss");
        this.video = json.getString("vid");
        if (Objects.equals(accessKey, this.accessKey)) {
          this.loadVideoWithMcp();
        } else {
          this.accessKey = accessKey;
          AnvatoConfig.createNew(context.getCurrentActivity(), this.accessKey, "", new JSONObject(), this);
          if (this.sdkReady) {
            this.loadVideoWithMcp();
          }
        }
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
  }

  private void loadVideoWithMcp() {
    if (this.token != null && this.video != null && this.sdkReady) {
      anvatoSDK.video.load(this.video, AnvatoGlobals.VideoType.VIDEO_TYPE_MCP, AnvatoPlaybackOptions.getInstance(), this.token);
    }
  }

  @Override
  public boolean onDataEvent(AnvatoGlobals.DataEvent dataEvent, String s, Bundle bundle) {
    if (dataEvent == AnvatoGlobals.DataEvent.SDK_READY) {
      this.sdkReady = true;
      this.loadVideoWithMcp();
    }
    return false;
  }

  @Override
  public boolean onVideoEvent(AnvatoGlobals.VideoEvent videoEvent, Bundle bundle) {
    return false;
  }

  @Override
  public void created(AnvatoConfig anvatoConfig) {
    anvatoSDK = AnvatoSDK.getInstance(context.getCurrentActivity(), this, this);
    anvatoSDK.video.initPlayer(context.getCurrentActivity(), this.anvatoPlayerUI);
  }

  @Override
  public void onError(AnvatoSDKException e) {

  }
}
