/**
 * 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 MycartView from './MyCartView';
import { withDatabase } from '../../../Database/grocery';
import { NavigationStackScreenProps } from 'react-navigation-stack';

interface Iprops extends NavigationStackScreenProps {
    database: any;
}
interface Istate {
    text: number,
    remove: string,
    add: string,
    actualPrice: number,
    total: number,
    saving: number
}

class MycartContainer extends React.Component<Iprops,Istate> {
    constructor(props:Iprops) {
        super(props)
        this.state = {
            text: 5,
            remove: 'red',
            add: 'red',
            actualPrice: 265,
            total: 0,
            saving: 125
        }
    }
    toggleDrawer = () => {
        this.props.navigation.toggleDrawer()
    }
    textChange(data:number) {
        this.setState({
            text: data
        })
    }
    addCount = (productId:number) => {
        this.props.database.plusQuantity(productId)
        this.setState({
            text: this.state.text + 1,
            add: 'green',
            remove: 'red',
            total: this.state.text * this.state.actualPrice
        })
    }
    subtractCount = (productId:number, items:any) => {
        if (items === 1) {
            this.openAlert(productId)
        } else {
            this.props.database.minusQuantity(productId)
        }
        this.setState({
            text: this.state.text - 1,
            remove: 'green',
            add: 'red'
        });
    }

    openAlert = (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.minusQuantity(productId);
                        this.setState({
                            text: 0
                        })
                    }
                },
            ],
            { cancelable: false },
        );
        this.setState({
            remove: 'grey'
        });
    }

    goForDeliver = () => {
        this.props.navigation.navigate('GroceryDeliveryDetails')
    }

    render() {
        const total = (this.state.actualPrice + 49) - this.state.saving
        return (
            <MycartView
                toggleDrawer={this.toggleDrawer}
                text={this.state.text}
                addCount={this.addCount}
                subtractCount={this.subtractCount}
                add={this.state.add}
                remove={this.state.remove}
                actualPrice={this.state.actualPrice}
                goForDeliver={this.goForDeliver}
                cartItems={this.props.database.products}
                total={total}
                saving={this.state.saving}
            />
        )
    }
}
export default withDatabase(MycartContainer);
