import { HubConnectionState } from '@microsoft/signalr'
import { ChatHubTypes  } from "./chat-box.action.types"

const INITIAL_STATE = 
{
    inProgress: false,
    result: [],
    errorMessages: null,
    hubConnection: null,
    isConnected: false,
    sendingInProgress: false,
    imageSendingInProgress: false,
    oldResult: []
}

const chatNotificationReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case ChatHubTypes.CHAT_HUB_CONNECTION_START:
            return {
                ...currentState,
                inProgress: true,
                isConnected: false
            }
        case ChatHubTypes.CHAT_HUB_CONNECTED:
            {
                const conn = action.payload
                return {
                    ...currentState,
                    inProgress: false,
                    hubConnection: conn,
                    isConnected: conn.state === HubConnectionState.Connected
                }
            }
            case ChatHubTypes.CHAT_HUB_CONNECTION_FAILURE:{
                const { message } = action.payload
                return {
                    ...currentState,
                    inProgress: false,
                    errorMessages: message,
                    hubConnection: null,
                    isConnected: false
                }
            }
            case ChatHubTypes.CHAT_MESSAGE_RECEIVED:
            
                    { 

                    
                    const conn = currentState.hubConnection
                    const { user ,message} = action.payload
                    /*
message:{
    imageUrl: null
    message: "MR Krishnendu Das has just joined the group"
    messageDate: "2021-07-28"
    messageType: 51
    timeStamp: "4:20:13 AM"
}
user:{
    profilePicUrl: null
    userName: "MR Krishnendu Das"
    userType: 7
}
                    */

                    const messageObj = {                        
                        FormattedTimeString: message.timeStamp ,
                        Message:message.message,
                        UserType: user.userType,
                        UserName: user.userName,
                        UserIcon: user.profilePicUrl ?? undefined,
                        MessageType: message.messageType,
                        ImageUrl: message.imageUrl
                    }

                    const lastItem = currentState.result[currentState.result.length - 1]
                    if (!!lastItem && lastItem.FormattedTimeString === messageObj.FormattedTimeString){
                        return {
                            ...currentState
                        }
                    }

                    return {
                        ...currentState,
                        isConnected: conn.state === HubConnectionState.Connected,
                        result : [...currentState.result, messageObj],
                }
            }
            case ChatHubTypes.CHAT_MESSAGE_SEND_START:{
                return {
                    ...currentState,
                    sendingInProgress: true
                }
            }
            
            case ChatHubTypes.CHAT_MESSAGE_SENT:{
                return {
                    ...currentState,
                    sendingInProgress: false
                }
            }
            
            case ChatHubTypes.CHAT_MESSAGE_SEND_FAILURE:{
                return {
                    ...currentState,
                    sendingInProgress: false,
                    errorMessages: action.payload.message
                }
            }

            case ChatHubTypes.CHAT_IMAGE_SEND_START:{
                return {
                    ...currentState,
                    imageSendingInProgress: true,                    
                }
            }

            case ChatHubTypes.CHAT_IMAGE_SENT: {
                return {
                    ...currentState,
                    imageSendingInProgress: false
                }
            }

            case ChatHubTypes.FETCH_CHAT_HISTORY_START:{
                return {
                    ...currentState,
                    inProgress: true
                }
            }
            /*
        [{
            "ChatId": 16,
            "MessageType": 51,
            "Message": "DR Debasish Basu has just joined the group",
            "ImageUrl": null,
            "UserId": "2b26dbe7-9309-48ca-96c8-7b56ad595bd7",
            "UserName": "DR Debasish Basu",
            "UserType": 6,
            "GroupName": "MSCFXMAMTC",
            "ProfilePicUrl": "2b26dbe7-9309-48ca-96c8-7b56ad595bd7.jpg",
            "CreatedOn": "2021-09-23T07:00:37.806407Z"
        },...]
            */
            case ChatHubTypes.FETCH_CHAT_HISTORY_SUCCESS:{
                const {records} = action.payload
                return {
                    ...currentState,
                    inProgress: false,
                    oldResult: records.reverse(),
                    errorMessages: null
                }
            }
            case ChatHubTypes.CHAT_HUB_RESET_STATE:{
                return INITIAL_STATE
            }

            case ChatHubTypes.CHAT_HUB_CLEAR_ERROR_STATE:{
                return {
                    ...currentState,                    
                    errorMessages: null,
                    sendingInProgress: false,
                }
            }

        default: 
            return currentState
    }
}

export default chatNotificationReducer