/**
 * React Native Full App (https://store.enappd.com/product/react-native-full-app)
 *
 * Copyright © 2019-present Enappd. All rights reserved.
 *
 * This source code is licensed as per the terms found in the
 * LICENSE.md file in the root directory of this source tree.
 */

/* eslint-disable no-this-before-super */
import React, { Component } from 'react';
import ProductListView from './productListView';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { selectProduct } from '../../../redux/actions/Product';
import { NavigationStackScreenProps } from 'react-navigation-stack';
import { AppReduxState } from '../../../redux/reducers';

interface Iprops extends NavigationStackScreenProps {
  categoryProductList: any;
  categoryTitle: string;
  actions: any;
  cartItemLength: number;
}

interface Istates {
  segment: boolean;
  button: boolean;
}

class ProductListContainer extends Component<Iprops, Istates> {

  constructor(props:Iprops) {
      super(props);
      this.state = {
          segment: false,
          button: true,
        };
    }
  goBack = () => {
      const { navigation } = this.props;
      navigation.goBack();
    }
  goForProductdetails = (items:any) => {
      const { navigation , actions } = this.props;
      actions.selectProduct(items);
      navigation.push('Productdetails');
    }
  goToMyCart = () => {
      this.props.navigation.navigate('Mycart');
    }
  render() {
      const { categoryProductList, categoryTitle, cartItemLength } = this.props;
      return (
            <ProductListView
             cartItemLength={cartItemLength}
             products={categoryProductList}
             goBack={this.goBack}
             title={categoryTitle}
             goForProductdetails={this.goForProductdetails}
             goToMyCart={this.goToMyCart}
            />
        );
    }

}

const mapStateToProps = (state:AppReduxState) => {
  return{
      categoryProductList : state.home.categoryProductList,
      categoryTitle : state.home.categoryTitle,
      cartItemLength: state.cart.cartItemNumber,
    };
};
const matchDispatchToProps = (dispatch:any) => ({
    actions: bindActionCreators({ selectProduct }, dispatch),
  });

export default connect(mapStateToProps, matchDispatchToProps)(ProductListContainer);
