import axios from 'axios';
import { integrationActionTypes } from '../../action-types/integrations-actions.types';

/**
 * Create new webhook
 * @param {*} building apps
 */
export const createWebhook = (formId, webhookDetails) => async () => {
  const link = `forms/${formId}/webhook`;
  try {
    const response = await axios.post(link, webhookDetails);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const updateWebhookUrl = (formId, details) => async dispatch => {
  const url = `forms/${formId}/webhook`;
  try {
    const response = await axios.put(url, details);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: integrationActionTypes.UPDATE_WEBHOOK_URL,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const getWebhookUrl = formId => async dispatch => {
  const url = `forms/${formId}/webhook`;
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: integrationActionTypes.GET_WEBHOOK_URL,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const removeWebhook = formId => async () => {
  const url = `forms/${formId}/webhook`;
  try {
    const response = await axios.delete(url);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

// GOOGLE SHEET INTEGRATION
export const getRedirectUrl = formId => async () => {
  const url = `forms/${formId}/google-sheet`;
  try {
    const redirectUrl = await axios.get(url);
    const { data, success } = redirectUrl.data;
    if (success) {
      window.location.replace(
        data.url.replace(
          'https%3A%2F%2Fuseformsadmin.bluegreensoft.com%2Fgoogle%2Fcallback',
          'http%3A%2F%2Flocalhost:3000%2Fgoogle-sheet%2Fcallback'
        )
      );
      // window.location.replace('https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?state=IW-tBJpjV0KS7UCoVwYNWjg1nJDjA&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&access_type=offline&prompt=consent&response_type=code&client_id=368494232530-g2e059o8plgjqdjgi7jusn7vhukr7poi.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fgoogle-sheet%2Fcallback&flowName=GeneralOAuthFlow');
      return redirectUrl.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const completeIntegration = (details, callback) => async () => {
  const url = '/forms/google-sheet/callback';
  try {
    const pushConfig = await axios.post(url, details);
    const { success } = pushConfig.data;
    if (success) {
      await sessionStorage.setItem('googleSheetInstallation', true);
      callback();
    }
    return pushConfig.data;
  } catch (error) {
    return error;
  }
};

export const removeGoogleSheet = formId => async dispatch => {
  const url = `forms/${formId}/google-sheet`;
  try {
    const removeInstallation = await axios.delete(url);
    const { success, data } = removeInstallation.data;
    if (success) {
      sessionStorage.setItem('googleSheetInstallation', false);
      dispatch({
        type: integrationActionTypes.UNINSTALL_APP_VIA_GOOGLE_SHEETS,
        payload: data,
      });
      return removeInstallation.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

// POWERBI INTEGRATION

export const getWorkspaceUrl = formId => async () => {
  const url = `forms/${formId}/powerbi`;
  try {
    const workspaceUrl = await axios.get(url);
    const { data, success } = workspaceUrl.data;
    if (success) {
      window.location.replace(data.url);
      return workspaceUrl.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response;
    }
  }
};

export const completeAuthenticationPowerBi = (
  details,
  callback
) => async () => {
  const url = '/forms/powerbi/callback';
  try {
    const pushConfig = await axios.post(url, details);
    const { success } = pushConfig.data;
    if (success) {
      await sessionStorage.setItem('powerbiIntegration', true);
      callback();
    }
    return pushConfig.data;
  } catch (error) {
    return error;
  }
};

export const removePowerBi = formId => async dispatch => {
  const url = `forms/${formId}/powerbi`;
  try {
    const removeInstallation = await axios.delete(url);
    const { success, data } = removeInstallation.data;
    if (success) {
      sessionStorage.setItem('powerbiIntegration', false);
      dispatch({
        type: integrationActionTypes.UNINSTALL_POWERBI_INTEGRATION,
        payload: data,
      });
      return removeInstallation.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

// API KEYS
export const generateApiKey = () => async () => {
  const url = 'api-key/generate';
  try {
    const response = await axios.post(url);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const getApiKeys = () => async dispatch => {
  const url = 'api-key';
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: integrationActionTypes.GET_GENERATED_APIKEYS,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const deleteApiKey = () => async dispatch => {
  const url = 'api-key';
  try {
    const response = await axios.delete(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: integrationActionTypes.DELETE_APIKEYS,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const resetApiKey = () => async () => {
  const url = 'api-key/reset';
  try {
    const response = await axios.post(url);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
