package minhhai2209.jirapluginconverter.plugin.setting;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import com.atlassian.oauth.consumer.ConsumerService;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.sal.api.transaction.TransactionTemplate;
import com.atlassian.upm.api.license.PluginLicenseManager;

import minhhai2209.jirapluginconverter.connect.descriptor.Descriptor;
import minhhai2209.jirapluginconverter.connect.descriptor.Modules;
import minhhai2209.jirapluginconverter.utils.ExceptionUtils;
import minhhai2209.jirapluginconverter.utils.JsonUtils;

public class PluginSetting {

  public static final String GROUP_ID = "generated_group_id";

  public static final String ARTIFACT_ID = "generated_artifact_id";

  public static final String PLUGIN_KEY = GROUP_ID + "." + ARTIFACT_ID;

  public static final String URL_SAFE_PLUGIN_KEY = GROUP_ID + "-" + ARTIFACT_ID;

  private static Descriptor descriptor;

  private static Plugin plugin;

  public static void load(
      PluginSettingsFactory pluginSettingsFactory,
      TransactionTemplate transactionTemplate,
      PluginLicenseManager pluginLicenseManager,
      ConsumerService consumerService) throws Exception {
    readDescriptor();
    KeyUtils.loadJiraConsumer(consumerService);
    KeyUtils.generateSharedSecret(pluginSettingsFactory, transactionTemplate);
  }

  private static void readDescriptor() {
    InputStream is = null;
    try {
      is = PluginSetting.class.getResourceAsStream("/imported_atlas_connect_descriptor.json");
      String descriptorString = IOUtils.toString(is);
      descriptor = JsonUtils.fromJson(descriptorString, Descriptor.class);
      WebItemUtils.buildWebItemLookup();
      WebPanelUtils.buildWebPanelLookup();
      PageUtils.buildGeneralPageLookup();
      PageUtils.buildAdminPageLookup();
      PageUtils.buildConfigurePageLookup();
      TabPanelUtils.buildJiraIssueTabPanelLookup();

      plugin = new Plugin();
      plugin.setName(descriptor.getName());
      plugin.setBaseUrl(getPluginBaseUrl());
    } catch (Exception e1) {
      if (is != null) {
        try {
          is.close();
        } catch (Exception e2) {
          ExceptionUtils.throwUnchecked(e2);
        }
      }
      ExceptionUtils.throwUnchecked(e1);
    }
  }

  public static Descriptor getDescriptor() {
    return descriptor;
  }

  public static String getPluginBaseUrl() {
    return descriptor.getBaseUrl();
  }

  public static Modules getModules() {
    return descriptor.getModules();
  }

  public static Plugin getPlugin() {
    return plugin;
  }
}

@@ -54,76 +54,8 @@
 g;%0A%0A
-import java.io.InputStream;%0A%0Aimport org.apache.commons.io.IOUtils;%0A%0A
 impo
@@ -103,16 +103,16 @@
 ervice;%0A
+
 import c
@@ -291,17 +291,16 @@
 anager;%0A
-%0A
 import m
@@ -489,16 +489,16 @@
 nUtils;%0A
-
 import m
@@ -544,16 +544,83 @@
 onUtils;
+%0Aimport org.apache.commons.io.IOUtils;%0A%0Aimport java.io.InputStream;
 %0A%0Apublic
@@ -1254,24 +1254,88 @@
 scriptor();%0A
+    LicenseUtils.setPluginLicenseManager(pluginLicenseManager);%0A
     KeyUtils

support licensing