import React from 'react';
import SummaryContainer from './summary_container';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {getCart} from 'mini_cart/selectors';
import './order_summary_container.scss';
import CheckoutItemBuilder from './checkout_item_builder';

const OrderSummaryContainer = props => {
  const data = props.cart.attributes;
  const enrolled = props.cart.attributes.reward_summary.enrolled_for_loyalty;
  const reward_points = isNaN(props.cart.attributes.reward_points)
    ? Number(props.cart.attributes.reward_points.human)
    : Number(props.cart.attributes.reward_points);

  return (
    <div>
      {data.order_items.length > 0 && (
        <>
          <article className="order-summary-container-new">
            <section className="order-summary-section">
              {props.isMobile && (
                <div
                  className="pull-left"
                  onClick={props.onOrderSummaryBackClick}
                >
                  <span className="moda-icon-arrow_sm_left" />
                  <span className="return-link margin-left-10">
                    Return to Checkout
                  </span>
                </div>
              )}
              <header className="order-summary-title-new">
                {' '}
                Order Summary{' '}
              </header>
              <div className="order-summary-products">
                {data.unavailable_order_items.length > 0 && (
                  <div>
                    {props.isMobile && (
                      <div className="order-summary-products_title">
                        Unavailable
                      </div>
                    )}
                    <CheckoutItemBuilder
                      productsArray={data.unavailable_order_items}
                      showCorrectPrice={showCorrectPrice}
                      feature_flags={props.feature_flags}
                      isMobile={props.isMobile}
                    />
                  </div>
                )}
                {data.ready_to_ship_order_items.length > 0 && (
                  <div>
                    {props.isMobile && (
                      <div className="order-summary-products_title">
                        Available Now
                      </div>
                    )}
                    <CheckoutItemBuilder
                      productsArray={data.ready_to_ship_order_items}
                      showCorrectPrice={showCorrectPrice}
                      feature_flags={props.feature_flags}
                      isMobile={props.isMobile}
                    />
                  </div>
                )}
                {data.on_order_items.length > 0 && (
                  <div>
                    {props.isMobile && (
                      <div className="order-summary-products_title">
                        Preorder
                      </div>
                    )}
                    <CheckoutItemBuilder
                      productsArray={data.on_order_items}
                      showCorrectPrice={showCorrectPrice}
                      feature_flags={props.feature_flags}
                      isMobile={props.isMobile}
                    />
                  </div>
                )}
              </div>
              <div className="order-summary-section_divider" />
              <SummaryContainer
                cart={props.cart}
                onAddPromoCode={props.onAddPromoCode}
                onSetPromoCode={props.onSetPromoCode}
                shipping_address_country_code={
                  props.shipping_address_country_code
                }
              />
            </section>
          </article>
          {props.feature_flags['loyalty-rewards'] &&
            enrolled &&
            // eslint-disable-next-line no-undef
            SiteConstants.current_user.is_employee == 'false' &&
            reward_points > 0 && (
              <div>
                <div className="earn-points">
                  <span className="border" />
                  Place your order to earn&nbsp;
                  <span>{reward_points} points!</span>
                  <span className="border" />
                </div>
              </div>
            )}
        </>
      )}
    </div>
  );
};

function showCorrectPrice(price) {
  return `${price.currency_html_code}${price.human}`;
}

const mapStateToProps = state => {
  return {
    cart: getCart(state),
    feature_flags: state.featureDecisions.feature_flags
  };
};

OrderSummaryContainer.propTypes = {
  cart: PropTypes.object.isRequired,
  feature_flags: PropTypes.object,
  onAddPromoCode: PropTypes.func,
  onSetPromoCode: PropTypes.func,
  onOrderSummaryBackClick: PropTypes.func,
  isMobile: PropTypes.boolean,
  shipping_address_country_code: PropTypes.string
};

export default connect(mapStateToProps)(OrderSummaryContainer);
