text: /*
   *   Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
   *  Licensed under the Apache License, Version 2.0 (the "License").
   *  You may not use this file except in compliance with the License.
   *  A copy of the License is located at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  or in the "license" file accompanying this file. This file is distributed
   *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
   *  express or implied. See the License for the specific language governing
   *  permissions and limitations under the License.
   */
  import React from 'react';
  import Amplify from 'aws-amplify';
  import { Auth, API } from 'aws-amplify';
  import awsConfig from '../amplify-config';
  import '../public/css/app.css';
  import '../public/css/gridforms.css';
  import logo from '../public/images/LTLogo.png';
  import CreateCustomerPopup  from './CreateCustomerPopup';
  import CreateProductPopup  from './CreateProductPopup';
  import CreateOrder  from './CreateOrder';
  import CreateVariant  from './CreateVariant';
  import Accordian  from './Accordian';
  import { withRouter, Link } from 'react-router-dom';
  import {NotificationContainer, NotificationManager} from 'react-notifications';
  
  
  
  const customersAPI = 'CustomersAPI';
  const getAllPath = '/all';
  
  const productsAPI = 'ProductsAPI';
  
  const createPath = '/create';
  const orderAPI = 'OrderAPI';
  
  class MainApp extends React.Component {
    constructor(props) {
      super(props);
  	
  	Amplify.Logger.LOG_LEVEL = 'DEBUG';
      
      this.state = {
        authToken: null,
        idToken: null
      };
  	
    }
  
  
    async componentDidMount() {
      const session = await Auth.currentSession();
  	sessionStorage.setItem('session', JSON.stringify(session));
      this.setState({ authToken: session.accessToken.jwtToken });
      this.setState({ idToken: session.idToken.jwtToken });
      this.getCustomers();
      this.getProducts();
  	
    }
    
    componentWillUnmount() {
          localStorage.clear();
      }
  
    /**
     * Determines if the API is enabled
     *
     * @return {Boolean} true if API is configured
     */
    hasApi() {
       const api = awsConfig.API.endpoints.filter(v => v.endpoint !== '');                                                   
       return (typeof api !== 'undefined');
    }
  
    
    async getProductConfig(id) {
      const apiRequest = {
        headers: {
          'Authorization': this.state.idToken,
          'Content-Type': 'application/json'
        }
      };
      return await API.get(productsAPI, '/'+id, apiRequest)
    }  
  
    async getCustomers() {
      const apiRequest = {
        headers: {
          'Authorization': this.state.idToken,
          'Content-Type': 'application/json'
        }
      };
      API.get(customersAPI, getAllPath, apiRequest).then(response => {
  		this.setState({customers: response.body});
  	}).catch(error => {
  		console.log(error.response)
  		if(error.response.status == 401){
  			alert('Please Sign In')
  			//this.props.history.replace('/signin');
  		}
  	});
    }
    
    async getProducts() {
  	  const apiRequest = {
          headers: {
            'Authorization': this.state.idToken,
            'Content-Type': 'application/json'
          }
        };
  	  API.get(productsAPI, getAllPath, apiRequest).then(response => {
  		this.setState({products: response.body});
  	  }).catch(error => {
  		console.log(error.response)
  		if(error.response.status == 401){
  			alert('Please Sign In')
  			//this.props.history.replace('/signin');
  		}
  	});
    }
  
    generateCustomerList() {
      var customers = [];
      if(this.state.customers){
          customers = JSON.parse(this.state.customers);
      } 
      
      let customerOptions = [];
      try{
          customerOptions = customers.map((customer) =>
  		{return {value:customer.ID, label: customer.Name}}
              );
      }catch(err)
      {
          console.log('Error rendering Customers: '+err);
      }
      return customerOptions;
        
    }
    
    parseProducts() {   
  	var products = [];
  	if(this.state.products){
         products = JSON.parse(this.state.products);
  	} 
  	
  	var productOptions = [];
      try{
          productOptions = products.map((product) =>
  		{return {value:product.ID, label: product.Name}}
              );
      }catch(err)
      {
          console.log('Error rendering Products: '+err);
      }
      return productOptions;
     }  
    
    render() {    
      return (
      <div className="app">
        <header>
          <img src={logo}/>
        </header> 
  	  <Accordian>
  		<div label="Create Order">
  			<CreateOrder customers={this.generateCustomerList()} products={this.parseProducts()} />
  		</div>	
  		<div label="Create Customer">
  			<CreateCustomerPopup get_all_customers={this.getCustomers.bind(this)}/>
  		</div>	
          <div label='Create Product'>
            <CreateProductPopup get_all_products={this.getProducts.bind(this)} />
  		</div>
  		<div label='Create Variant'>
            <CreateVariant customers={this.generateCustomerList()} products={this.parseProducts()} />
  		</div>		
        </Accordian>	  
        </div>
        );
    }
  }
  
  export default withRouter(MainApp);
  