import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import PaymentSummaryRow from './payment_summary_row';
import NewPromotionContainer from './promotion_container';
import RewardContainer from './reward_container';
import {getAddresses} from '../../selectors';
import './summary_container.scss';

class SummaryContainer extends PureComponent {
  static propTypes = {
    cart: PropTypes.object,
    onAddPromoCode: PropTypes.func,
    onSetPromoCode: PropTypes.func,
    shipping_address_country_code: PropTypes.string,
    addresses: PropTypes.object
  };
  constructor(props) {
    super(props);

    this.state = {
      show_affirm: this.showAffirm(),
      show_not_available_affirm: this.showNotAvailableAffirm(),
      isHoveringOnTooltip: false
    };
  }

  componentDidMount() {
    if (typeof affirm != 'undefined') {
      // eslint-disable-next-line no-undef
      affirm.ui.ready(function() {
        // eslint-disable-next-line no-undef
        affirm.ui.refresh();
      });
    }
  }

  componentDidUpdate(prevProps) {
    if (prevProps.cart.attributes != this.props.cart.attributes) {
      this.setState({
        show_affirm: this.showAffirm(),
        show_not_available_affirm: this.showNotAvailableAffirm()
      });
    }
    if (typeof affirm != 'undefined') {
      // eslint-disable-next-line no-undef
      affirm.ui.ready(function() {
        // eslint-disable-next-line no-undef
        affirm.ui.refresh();
      });
    }
  }

  showAffirm() {
    return (
      this.baseForShowAffirm() &&
      parseInt(this.props.cart.attributes.payment_summary.pay_later.price) == 0
    );
  }

  showNotAvailableAffirm() {
    return (
      this.baseForShowAffirm() &&
      parseInt(this.props.cart.attributes.payment_summary.pay_later.price) != 0
    );
  }

  baseForShowAffirm() {
    return (
      this.props.cart &&
      parseInt(this.props.cart.attributes.payment_summary.pay_today.price) !=
        0 &&
      // eslint-disable-next-line no-undef
      SiteConstants.current_country_code === 'USA' &&
      this.props.cart.attributes.payment_summary.card_payment_today.price <=
        25000
    );
  }

  renderSiteCreditRow(payment_summary) {
    if (
      payment_summary.site_credit &&
      parseFloat(payment_summary.site_credit.price) > 0
    ) {
      return (
        <div>
          <PaymentSummaryRow
            title="Site Credit"
            currency={
              payment_summary.site_credit &&
              payment_summary.site_credit.currency_html_code
            }
            value={
              payment_summary.site_credit && payment_summary.site_credit.human
            }
            negative
            text_style="mojo-tagline-price-light font-size-12"
            label_class="site-credit mojo-tagline-price-light font-size-12"
          />
        </div>
      );
    } else {
      return false;
    }
  }

  renderAffirmMsg(payment_summary) {
    if (this.state.show_affirm) {
      const shipping_address_country_code = this.props
        .shipping_address_country_code;
      if (
        (!(shipping_address_country_code == 'US') &&
          // eslint-disable-next-line no-undef
          _.isEmpty(this.props.addresses)) ||
        shipping_address_country_code == 'US'
      ) {
        // Boutique Case
        return (
          <p
            className="affirm-as-low-as"
            data-page-type="cart"
            data-amount={
              payment_summary.card_payment_today &&
              payment_summary.card_payment_today.price * 100
            }
          />
        );
      } else {
        //Non US case
        return (
          <div className="affirm-not-available">
            <img src="affirm-pay.png" height="16x" width="41px" />
            monthly payment is not available outside of the US
          </div>
        );
      }
    }
  }

  render() {
    const payment_summary = this.props.cart.attributes.payment_summary;
    let free_shipping_message = '';
    if (payment_summary.shipping_promotion_applied) {
      // eslint-disable-next-line no-undef
      if (SiteConstants.current_user.is_high_value_stylist_client) {
        free_shipping_message = 'Stylist Client Free Shipping';
      }
    }
    return (
      <div id="payment-summary-container" className="">
        <div className="order-summary-container-v2">
          <PaymentSummaryRow
            title="Order Subtotal"
            currency={
              payment_summary &&
              payment_summary.cart_total &&
              payment_summary.cart_total.currency_html_code
            }
            value={
              payment_summary &&
              payment_summary.cart_total &&
              payment_summary.cart_total.human
            }
            label_class="mojo-tagline-price-light"
          />
          {!payment_summary.vat_applicable && (
            <PaymentSummaryRow
              title={payment_summary && payment_summary.display_tax_type}
              currency={
                payment_summary.total_taxes &&
                payment_summary.total_taxes.currency_html_code
              }
              value={
                payment_summary.total_taxes && payment_summary.total_taxes.human
              }
              label_class="mojo-tagline-price-light"
              message_text="The amount of sales tax due depends on the sales tax laws of the location to which you are shipping."
            />
          )}
          {!payment_summary.shipping_promotion_applied && (
            <PaymentSummaryRow
              title="Shipping"
              currency={
                payment_summary.shipping_and_packaging &&
                payment_summary.shipping_and_packaging.currency_html_code
              }
              value={
                payment_summary.shipping_and_packaging &&
                payment_summary.shipping_and_packaging.human
              }
              label_class="mojo-tagline-price-light"
            />
          )}
          {payment_summary.shipping_promotion_applied && (
            <PaymentSummaryRow
              title="Shipping"
              value={free_shipping_message}
              label_class="mojo-tagline-price-light"
            />
          )}
        </div>

        <NewPromotionContainer
          cart={this.props.cart}
          payment_summary={payment_summary}
          onAddPromoCode={this.props.onAddPromoCode}
          onSetPromoCode={this.props.onSetPromoCode}
        />

        <RewardContainer
          payment_summary={payment_summary}
          cart={this.props.cart}
        />
        {this.renderSiteCreditRow(payment_summary)}

        <PaymentSummaryRow
          title="Order Total"
          currency={payment_summary.order_total.currency_html_code}
          value={
            payment_summary.order_total && payment_summary.order_total.human
          }
          container_class="container-payment-normal order-total"
          label_class="mojo-tagline-price-light"
          message_text="Preorder items require a 50% deposit at the time of purchase. The remaining balance and sales tax/duties will be charged upon shipping."
        />

        <div className="order-summary-container-v2 last-container">
          <PaymentSummaryRow
            title="Pay Today"
            currency={payment_summary.card_payment_today.currency_html_code}
            value={
              payment_summary.card_payment_today &&
              payment_summary.card_payment_today.human
            }
            label_class="pay-today mojo-tagline-price"
          />

          <PaymentSummaryRow
            title="Remaining Balance"
            currency={
              payment_summary.pay_later &&
              parseFloat(payment_summary.pay_later.price) > 0 &&
              payment_summary.pay_later.currency_html_code
            }
            value={
              payment_summary.pay_later &&
              parseFloat(payment_summary.pay_later.price) > 0 &&
              payment_summary.pay_later.human
            }
            label_class="mojo-tagline-price-light"
            message_text="Preorder items require a 50% deposit at the time of purchase. The remaining balance and sales tax/duties will be charged upon shipping."
          />
          <div className="order-summary-wrap-v2 column row">
            {this.renderAffirmMsg(payment_summary)}
          </div>
        </div>
      </div>
    );
  }
}

const mapStateToProps = state => {
  return {
    addresses: getAddresses(state)
  };
};

export default connect(mapStateToProps)(SummaryContainer);
