import React from 'react';
import './SelectAppPlatform.css';
import axios from 'axios';
import {connect} from 'react-redux';
import * as Constants from '../../constants';
import Box from '../../components/UI/Box/Box';
import Button from '../../components/UI/Button/Button';
import {
  downloadItem,
  setAllowDownload
} from '../../redux/actions/download_actions';
import DropDown from '../../components/UI/DropDown/DropDown';
import IPCContainer from '../IPCContainer/IPCContainer';
import {notifyError} from '../../redux/actions/error_actions';
import {setInstallTestActive} from '../../redux/actions/install_actions';

class SelectAppPlatform extends IPCContainer {
  state = {
    Selected: 0,
  };

  grabLatestRelease = appPlatform => {
    const errorHandler = error => {
      this.props.notifyError(error);
      this.props.setInstallTestActive(false);
      this.props.setAllowDownload(false);
    };
    axios
      .get(Constants.RELEASES_URL)
      .then(response => {
        try {
          const releases = response.data.Versions.Release[appPlatform];
          const latestVersion = releases[releases.length - 1];
          const release = response.data.Locations[appPlatform][latestVersion];
          this.props.downloadItem(latestVersion + '.zip', Constants.INSTALL_TYPES.TestRelease, release.urls, false, latestVersion, appPlatform);
        } catch (error) {
          errorHandler(error);
        }
      })
      .catch(error => {
        errorHandler(error);
      });
  };

  handleTestClicked = () => {
    this.props.setInstallTestActive(true);
    this.props.setAllowDownload(true);
    this.grabLatestRelease(Constants.LINUX_SELECTABLE_PLATFORMS[this.state.Selected])
  };

  handleChanged = e => {
    this.setState({
      ...this.state,
      Selected: Constants.LINUX_SELECTABLE_PLATFORMS.indexOf(e.target.value),
    });
  };

  render() {
    return (
      <Box dxDark dxStyle={{padding: 'var(--default_spacing)'}}>
        <h1 className={'SAPHeading'}>Select Linux Platform</h1>
        <div className={'SAPContent'}>
          <p>Repertory was unable to detect your Linux distribution. Please select one of the following and click <b>Test</b> to continue:</p>
        </div>
        <div className={'SAPActions'}>
          <DropDown changed={this.handleChanged}
                    disabled={this.props.InstallTestActive}
                    items={Constants.LINUX_SELECTABLE_PLATFORMS}
                    selected={Constants.LINUX_SELECTABLE_PLATFORMS[this.state.Selected]}/>
          <Button clicked={this.handleTestClicked}
                  disabled={this.props.InstallTestActive}>Test</Button>
        </div>
      </Box>
    );
  }
}

const mapStateToProps = state => {
  return {
    InstallTestActive: state.install.InstallTestActive,
  }
};

const mapDispatchToProps = dispatch => {
  return {
    downloadItem: (name, type, urls, isWinFSP, testVersion, appPlatform) => dispatch(downloadItem(name, type, urls, isWinFSP, testVersion, appPlatform)),
    notifyError: msg => dispatch(notifyError(msg)),
    setAllowDownload: allow => dispatch(setAllowDownload(allow)),
    setInstallTestActive: active => dispatch(setInstallTestActive(active)),
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(SelectAppPlatform);