/**
 * 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.
 */
import React from 'react';
import { Alert } from 'react-native';
import HomeView from './homeView';
import { withDatabase } from '../../../Database/grocery';
import { NavigationDrawerScreenProps } from 'react-navigation-drawer';

interface Iprops extends NavigationDrawerScreenProps {
  database: any;
}
interface Istate {
  segment: boolean;
  button: boolean;
  text: any;
  remove: string;
  add: string;
  selectedIndex: number | null;
}

class HomeContainer extends React.Component<Iprops, Istate> {
  constructor(props: Iprops) {
    super(props);
    this.state = {
      segment: false,
      button: true,
      text: 0,
      remove: 'red',
      add: 'red',
      selectedIndex: null,
    };
  }
  componentDidMount() {
    this.props.navigation.addListener('willFocus', () => {
      this.setState({ button: !this.state.button });
    });
  }
  componentDidUpdate() {}
  toggleDrawer = () => {
    this.props.navigation.toggleDrawer();
  };

  goToMyCart = () => {
    this.props.navigation.navigate('GroceryMycart');
  };
  goToSearch = () => {
    this.props.navigation.navigate('GrocerySearch');
  };

  goforProductPage(products: any) {
    this.props.navigation.navigate('GroceryProducts', { products });
  }
  goforProductDetailPage(items: any) {
    this.props.navigation.navigate('GroceryProductdetails', { items });
  }

  gotoAddItems = (sectionId: number, productId: number) => {
    this.props.database.addToCart(sectionId, productId);
    this.setState({
      button: !this.state.button,
    });
  };

  changeStateFunction(index: number) {
    const { selectedIndex } = this.state;
    if (index === selectedIndex) {
      this.setState({
        selectedIndex: null,
      });
    } else {
      this.setState({
        selectedIndex: index,
      });
    }
  }

  addCount = (sectionId: number, productId: number) => {
    this.props.database.addToCart(sectionId, productId);
    this.setState({
      // text: this.state.text += 1,
      add: 'green',
      remove: 'red',
      // Total: this.state.text * this.state.actualPrice
    });
  };
  subtractCount = (sectionId: number, productId: number, items: any) => {
    if (items === 1) {
      this.openAlert(sectionId, productId);
    } else {
      this.props.database.removeFromCart(sectionId, productId);
    }
    this.setState({
      button: !this.state.button,
    });
  };

  openAlert = (sectionId: number, productId: number) => {
    Alert.alert(
      'Remove From Cart!',
      'Are you Sure you want to remove this Product',
      [
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
          style: 'cancel',
        },
        {
          text: 'Remove',
          onPress: () => {
            this.props.database.removeFromCart(sectionId, productId);
            this.setState({
              button: !this.state.button,
            });
          },
        },
      ],
      { cancelable: false }
    );
    this.setState({
      remove: 'grey',
    });
  };

  goToViewAll(listItems: any, offerIndex: number) {
    this.props.navigation.navigate('GroceryViewmore', {
      listItems,
      offerIndex,
    });
  }
  render() {
    const { selectedIndex } = this.state;
    return (
      <HomeView
        toggleDrawer={this.toggleDrawer}
        goToMyCart={this.goToMyCart}
        goToSearch={this.goToSearch}
        goforProductPage={(products: any) => this.goforProductPage(products)}
        gotoAddItems={this.gotoAddItems}
        selectedIndex={selectedIndex}
        changeStateFunction={(i: number) => this.changeStateFunction(i)}
        goforProductDetailPage={(items: any) =>
          this.goforProductDetailPage(items)
        }
        addCount={this.addCount}
        subtractCount={this.subtractCount}
        add={this.state.add}
        remove={this.state.remove}
        inCartLength={this.props.database.products.length}
        products={this.props.database.shortOffers}
        goToViewAll={(listItems: any, offerIndex) =>
          this.goToViewAll(listItems, offerIndex)
        }
      />
    );
  }
}
export default withDatabase(HomeContainer);
