package minhhai2209.jirapluginconverter.plugin.lifecycle;

import org.apache.http.HttpHeaders;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import com.atlassian.event.api.EventPublisher;
import com.atlassian.plugin.Plugin;
import com.atlassian.plugin.event.PluginEventListener;
import com.atlassian.plugin.event.events.PluginEnabledEvent;
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.sal.api.transaction.TransactionTemplate;
import com.atlassian.upm.api.license.PluginLicenseManager;

import minhhai2209.jirapluginconverter.plugin.jwt.JwtComposer;
import minhhai2209.jirapluginconverter.plugin.setting.KeyUtils;
import minhhai2209.jirapluginconverter.plugin.setting.LifeCycleUtils;
import minhhai2209.jirapluginconverter.plugin.setting.PluginSetting;
import minhhai2209.jirapluginconverter.plugin.setting.SenUtils;
import minhhai2209.jirapluginconverter.plugin.utils.HttpClientFactory;
import minhhai2209.jirapluginconverter.utils.ExceptionUtils;
import minhhai2209.jirapluginconverter.utils.JsonUtils;

public class PluginLifeCycleEventListener implements InitializingBean, DisposableBean {

  private EventPublisher eventPublisher;

  private PluginSettingsFactory pluginSettingsFactory;

  private TransactionTemplate transactionTemplate;

  private ApplicationProperties applicationProperties;

  private PluginLicenseManager pluginLicenseManager;

  public PluginLifeCycleEventListener(
      EventPublisher eventPublisher,
      PluginSettingsFactory pluginSettingsFactory,
      TransactionTemplate transactionTemplate,
      ApplicationProperties applicationProperties,
      PluginLicenseManager pluginLicenseManager) {
    this.eventPublisher = eventPublisher;
    this.pluginSettingsFactory = pluginSettingsFactory;
    this.transactionTemplate = transactionTemplate;
    this.applicationProperties = applicationProperties;
    this.pluginLicenseManager = pluginLicenseManager;
  }

  @Override
  public void destroy() throws Exception {
    eventPublisher.unregister(this);
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    eventPublisher.register(this);
  }

  @PluginEventListener
  public void onPluginEnabled(PluginEnabledEvent enabledEvent) throws Exception {
    Plugin enabledPlugin = enabledEvent.getPlugin();
    String enabledPluginKey = enabledPlugin.getKey();
    if (PluginSetting.PLUGIN_KEY.equals(enabledPluginKey)) {
      PluginSetting.load(pluginSettingsFactory, transactionTemplate, pluginLicenseManager);
      String installedUrl = LifeCycleUtils.getInstalledUrl();
      if (installedUrl != null) {

        PluginLifeCycleEvent event = new PluginLifeCycleEvent();
        event.setBaseUrl(PluginSetting.getJiraBaseUrl());
        event.setClientKey(KeyUtils.getClientKey());
        event.setDescription("");
        event.setEventType(EventType.enabled);
        event.setKey(PluginSetting.PLUGIN_KEY);
        event.setPluginsVersion(enabledPlugin.getPluginInformation().getVersion());
        event.setProductType(ProductType.jira);
        event.setPublicKey(KeyUtils.getPublicKey());
        event.setServerVersion(applicationProperties.getVersion());
        event.setServiceEntitlementNumber(SenUtils.getSen());
        event.setSharedSecret(KeyUtils.getSharedSecret());

        sendInstall(installedUrl, event);
      }
    }
  }

  private void sendInstall(String installedUrl, PluginLifeCycleEvent event) {
    try {
      String installedUri = LifeCycleUtils.getInstalledUri();
      HttpClient httpClient = HttpClientFactory.build();
      HttpPost post = new HttpPost(installedUrl);
      String json = JsonUtils.toJson(event);
      post.setEntity(new StringEntity(json));
      post.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
      String jwt = JwtComposer.compose(KeyUtils.getClientKey(), KeyUtils.getSharedSecret(), "POST", installedUri, null, null);
      post.addHeader("Authorization", "JWT " + jwt);
      httpClient.execute(post);
    } catch (Exception e) {
      ExceptionUtils.throwUnchecked(e);
    }
  }
}

@@ -2492,24 +2492,36 @@
 Exception %7B%0A
+    try %7B%0A  
     Plugin e
@@ -2557,24 +2557,26 @@
 etPlugin();%0A
+  
     String e
@@ -2617,24 +2617,26 @@
 tKey();%0A    
+  
 if (PluginSe
@@ -2682,24 +2682,26 @@
 y)) %7B%0A      
+ 
+ 
 PluginSettin
@@ -2776,24 +2776,26 @@
 ger);%0A      
+  
 String insta
@@ -2834,24 +2834,26 @@
 alledUrl();%0A
+  
       if (in
@@ -2879,24 +2879,26 @@
  %7B%0A%0A        
+  
 PluginLifeCy
@@ -2942,32 +2942,34 @@
 vent();%0A        
+  
 event.setBaseUrl
@@ -2998,24 +2998,26 @@
 BaseUrl());%0A
+  
         even
@@ -3057,32 +3057,34 @@
 Key());%0A        
+  
 event.setDescrip
@@ -3089,24 +3089,26 @@
 iption(%22%22);%0A
+  
         even
@@ -3142,32 +3142,34 @@
 abled);%0A        
+ 
+ 
 event.setKey(Plu
@@ -3192,16 +3192,18 @@
 N_KEY);%0A
+  
         
@@ -3278,32 +3278,34 @@
 ion());%0A        
+  
 event.setProduct
@@ -3328,32 +3328,34 @@
 .jira);%0A        
+ 
+ 
 event.setPublicK
@@ -3375,32 +3375,34 @@
 etPublicKey());%0A
+  
         event.se
@@ -3445,32 +3445,34 @@
 .getVersion());%0A
+  
         event.se
@@ -3517,32 +3517,34 @@
 Sen());%0A        
+  
 event.setSharedS
@@ -3587,16 +3587,18 @@
         
+  
 sendInst
@@ -3629,17 +3629,56 @@
 ;%0A      
-%7D
+  %7D%0A      %7D%0A    %7D catch (Exception e) %7B%0A
 %0A    %7D%0A 

make enable listener silent on failure