

function orderUpdateReducer(state, { type, total, id, owner, goods, Id, images, name, description,  price, count }) {
    if(state === undefined){

            state = {}

    }

    if (type === "ORDER_ADD_ORDER") {
        state = {
            ...state,  
            owner: owner,
            total: total,        
            id: id,
            goods: goods
                
        }

    }

    if (type === "ORDER_DELETE_ONE") {
        if((state.goods[Id].count - 1) == 0 ){
            state = {
                ...state,
                goods: {
                    ...state.goods,

                },
                total: +state.total - state.goods[Id].price  
            }
            delete state.goods[Id]
        }
        else {
            state = {
                ...state,
                goods : {
                    ...state.goods,
                    [Id]: {
                        ...state.goods[Id],
                        count: +[state.goods[Id].count] - 1,

                    }
                },
                total: +state.total - state.goods[Id].price                   
            }
        }
    }
    
    if (type === "ORDER_ADD_ONE") {

        state = {
            ...state,
            goods : {
                ...state.goods,
                [Id]: {
                    ...state.goods[Id],
                    count: +[state.goods[Id].count] + 1,

                }
            },
            total: +state.total + state.goods[Id].price                   
        }
    }

    if (type === "ORDER_ADD_NEW_GOOD") {

        if(state.goods[Id]){
            state = {
                ...state,
                goods : {
                    ...state.goods,
                    [Id]: {
                        ...state.goods[Id],
                        count: +[state.goods[Id].count] + 1,
    
                    }
                },
                total: +state.total + state.goods[Id].price                   
            }
        }
        else {

            state = {
                ...state,
                goods: {
                    ...state.goods,
                    [Id]: {
                        count: (state.goods && state.goods[Id] ? state.goods[Id] : 0) + 1,
                        good: {
                            name: name,
                            images: images,
                            price: price,
                            _id : Id,

                        },
                        
                        _id : Id,
                        price: price,
                    }

                }
            }
        }
    }
    if (type === "ORDER_CLEAR") {

        state = {
            id: state.id,
            goods: {},
            total: 0,
            count: 0
            
        }
    }





    return state;
}
    



const actionOrderAddOrder = ( id, goods, total, owner, name, price, images, count = 0) => ({
    type: "ORDER_ADD_ORDER",
    id,
    goods,
    count,
    owner,
    price,
    name,
    images,
    total
    
});

const actionOrderDelete = (Id, id, goods, images) => ({
    type: "ORDER_DELETE_ONE",
    id,
    Id,
    goods,
    images

});
const actionOrderAddOne = (Id, id, goods, images) => ({
    type: "ORDER_ADD_ONE",
    id,
    Id,
    goods,
    images

});
const actionOrderAddNewGood = (Id, name, images, price) => ({
    type: "ORDER_ADD_NEW_GOOD",

    Id,
    name,

    images,
    price

});

const actionOrderClear = () => ({
    type:"ORDER_CLEAR"
})


// const actionCartClear = (count, price) => ({
//     type: "CART_CLEAR",
//     count,
//     price
// });
export {orderUpdateReducer, actionOrderClear, actionOrderAddNewGood, actionOrderAddOne, actionOrderAddOrder, actionOrderDelete};