import axios from 'axios';
import * as Constants from '../../constants';
import {createAction} from 'redux-starter-kit';
import {notifyError} from './error_actions';
import {setApplicationReady} from './common_actions';

export const CLEAR_UI_UPGRADE = 'relver/clearUIUpgrade';
export const clearUIUpgrade = () => {
  return {
    type: CLEAR_UI_UPGRADE,
    payload: null,
  };
};

export const detectUIUpgrade = () => {
  return (dispatch, getState) => {
    axios
      .get(Constants.UI_RELEASES_URL)
      .then(response => {
        const state = getState();
        const appPlatform = state.common.AppPlatform;
        const version = state.common.Version;
        const data = response.data;

        if (data.Versions &&
          data.Versions[appPlatform] &&
          (data.Versions[appPlatform].length > 0) &&
          (data.Versions[appPlatform][0] !== version)) {
          dispatch(setUIUpgradeData(data.Locations[appPlatform][data.Versions[appPlatform][0]]));
        } else {
          dispatch(clearUIUpgrade());
        }
      }).catch(error => {
        dispatch(clearUIUpgrade());
        dispatch(notifyError(error));
    });
  };
};

export const loadReleases = () => {
  return (dispatch, getState) => {
    const dispatchActions = (locationsLookup, versionLookup)=> {
      const state = getState().relver;
      const latestVersion = versionLookup[Constants.RELEASE_TYPES[state.Release]].length - 1;
      let version = state.Version;
      if ((version === -1) || !versionLookup[Constants.RELEASE_TYPES[state.Release]][version]) {
        version = latestVersion;
      }

      dispatch(setReleaseData(locationsLookup, versionLookup));
      if (version !== state.Version) {
        dispatch(setActiveRelease(state.Release, version));
      }
      dispatch(setReleaseUpgradeAvailable((version !== latestVersion)));
      dispatch(setApplicationReady(true));
    };

    axios
      .get(Constants.RELEASES_URL)
      .then(response => {
        const appPlatform = getState().common.AppPlatform;
        const versionLookup = {
          Release: response.data.Versions.Release[appPlatform],
          RC: response.data.Versions.RC[appPlatform],
          Beta: response.data.Versions.Beta[appPlatform],
          Alpha: response.data.Versions.Alpha[appPlatform],
        };
        const locationsLookup = {
          ...response.data.Locations[appPlatform],
        };

        localStorage.setItem('releases', JSON.stringify({
          LocationsLookup: locationsLookup,
          VersionLookup: versionLookup
        }));
        dispatchActions(locationsLookup, versionLookup);
      }).catch(error => {
        const releases = localStorage.getItem('releases');
        if (releases && (releases.length > 0)) {
          const obj = JSON.parse(releases);
          const locationsLookup = obj.LocationsLookup;
          const versionLookup = obj.VersionLookup;

          dispatchActions(locationsLookup, versionLookup);
        } else {
          dispatch(notifyError(error, true));
        }
      });
  };
};

export const SET_ACTIVE_RELEASE = 'relver/setActiveRelease';
export const setActiveRelease = (release, version) => {
  return {
    type: SET_ACTIVE_RELEASE,
    payload: {
      release,
      version
    }
  };
};

export const setDismissUIUpgrade = createAction('relver/setDismissUIUpgrade');
export const setInstalledVersion = createAction('relver/setInstalledVersion');

export const SET_RELEASE_DATA = 'relver/setReleaseData';
export const setReleaseData = (locationsLookup, versionLookup)=> {
  return {
    type: SET_RELEASE_DATA,
    payload: {
      locations: locationsLookup,
      versions: versionLookup,
    }
  }
};

export const setReleaseUpgradeAvailable = createAction('relver/setReleaseUpgradeAvailable');
export const setUIUpgradeData = createAction('relver/setUIUpgradeData');