#include    "goahead.h"
#include    "libipmi_IPM.h"
#include    "com_IPMIDefs.h"
#include    "ResultUtils.h"
#include    "cJSON.h"
#include    "libipmi_storlead_OEM.h"
#include    "libsensor.h"
#include    <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "com_BmcType.h"
#include "libipmi_usermgmt.h"

PUBLIC bool websVerifyPasswordFromBMC(Webs *wp)
{
    char    passbuf[ME_GOAHEAD_LIMIT_PASSWORD * 3 + 3];
    bool    success = TRUE;
    int 	wRet;
    IPMI20_UDS_SESSION_T    UDSSession;

    assert(wp);

//    printf("---> verify from BMC\n");
    
    //Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);
    
    if(LIBIPMI_AuthorVerify(&UDSSession, wp->username, wp->password, DEFAULT_TIMEOUT) == FALSE)
    {
    	success = FALSE;
    }
    //Close session
    LIBIPMI_CloseSession(&UDSSession );

    gChassisManageEn = 1;

    return success;
}

void getAllUserInfo(Webs *wp){
    int wRet;
    IPMI20_UDS_SESSION_T    UDSSession;
    UserInfo_T userInfoTbl[MAX_USER_NUM];
    int i;
    //Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);
    
    LIBIPMI_GetAllUserInfo(&UDSSession, userInfoTbl, DEFAULT_TIMEOUT );

    //Close session
    LIBIPMI_CloseSession(&UDSSession );

    char *pStr;
    int code = 200;
    cJSON * root =  cJSON_CreateObject();
    cJSON  *pJsonArry,*pJsonsub;
    pJsonArry=cJSON_CreateArray();   /*创建数组*/
    for(i=0;i<MAX_USER_NUM;i++)
    {
    	if(userInfoTbl[i].UserStatus == TRUE)
    	{
    		cJSON_AddItemToArray(pJsonArry,pJsonsub=cJSON_CreateObject());
    		cJSON_AddNumberToObject(pJsonsub, "userid", userInfoTbl[i].UserId); 
    		cJSON_AddStringToObject(pJsonsub, "username", userInfoTbl[i].UserName); 
   
    	}
    }

    cJSON_AddItemToObject(root, "data", pJsonArry);
    pStr = cJSON_PrintUnformatted(root);

//    printf("---> cJSON Str:\n%s\n", pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}

void setUserPassword(Webs *wp){
    int wRet;
    IPMI20_UDS_SESSION_T    UDSSession;
    char *username = websGetVar(wp, "username", NULL);
    //char *oldpassword = websGetVar(wp, "oldpassword", NULL);
    char *password = websGetVar(wp, "password", NULL);
    char *useridstr = websGetVar(wp, "userid", NULL);
    uint8_t userid = atoi(useridstr);

    // printf("---> setUserPassword, userid: %d, username: %s,  password: %s\n",
    // 	userid, username, password);
    //Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);

    // //verify old password
    // if(LIBIPMI_AuthorVerify(&UDSSession, username, oldpassword, DEFAULT_TIMEOUT) != TRUE)
    // {
    // 	printf("Verify oldpassword error!\n" );
    // 	//Close session
    // 	LIBIPMI_CloseSession(&UDSSession );
    // 	websError(wp, 404, "Verify oldpassword error!");
    // 	return;
    // }

    //printf("Verify oldpassword success!\n" );
    //set new password
    if(0 != LIBIPMI_SetPassword(&UDSSession, userid, password, DEFAULT_TIMEOUT))
    {
    	printf("Set user password fail!\n");
    	//Close session
    	LIBIPMI_CloseSession(&UDSSession );
    	websError(wp, 404, "Set user password fail!");    	
    	return;
    }
    //printf("Set user password success!\n" );
  
    //Close session
    LIBIPMI_CloseSession(&UDSSession );

    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);
    
   
    pStr = cJSON_PrintUnformatted(root);

 //   printf("---> cJSON Str:\n%s\n", pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}

void addUser(Webs *wp){
    int 			wRet;
    IPMI20_UDS_SESSION_T    UDSSession;
    UserInfo_T 		userInfoTbl[MAX_USER_NUM];
    uint8_t 		userid;
    int i;
    char *username = websGetVar(wp, "username", NULL);
    char *password = websGetVar(wp, "password", NULL);

    //Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);

    //Find valid userid
    LIBIPMI_GetAllUserInfo(&UDSSession, userInfoTbl, DEFAULT_TIMEOUT );
    for(i=0;i<MAX_USER_NUM;i++)
    {
    	if(userInfoTbl[i].UserStatus != TRUE)
    	{
    		userid = i+1;
    		break;
    	}
    }
    if(MAX_USER_NUM == i)
    {
    	printf("The user is full!\n");
    	websError(wp, 404, "The user is full!");
    	//Close session
    	LIBIPMI_CloseSession(&UDSSession );
    	return;
    }
    
    //set user name
   	if(0 != LIBIPMI_SetUser(&UDSSession, username, userid, DEFAULT_TIMEOUT ))
   	{
   		printf("Set user name fail!\n");
    	//Close session
    	LIBIPMI_CloseSession(&UDSSession );
    	websError(wp, 404, "Set user name fail!");    	
    	return;
   	}
    
    //set user password
    if(0 != LIBIPMI_SetPassword(&UDSSession, userid, password, DEFAULT_TIMEOUT))
    {
    	printf("Set user password fail!\n");
    	//Close session
    	LIBIPMI_CloseSession(&UDSSession );
    	websError(wp, 404, "Set user password fail!");    	
    	return;
    }

    //Close session
    LIBIPMI_CloseSession(&UDSSession );

    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);
    // cJSON_AddNumberToObject(data, "userid", userid);
    // cJSON_AddStringToObject(data, "username", username);
    // cJSON_AddStringToObject(data, "password", password);
   
    pStr = cJSON_PrintUnformatted(root);

//    printf("---> cJSON Str:\n%s\n", pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}

void delUser(Webs *wp){
    int wRet;
    IPMI20_UDS_SESSION_T    UDSSession;
    char *useridstr = websGetVar(wp, "userid", NULL);
    uint8_t userid = atoi(useridstr);
    char UserName[MAX_USER_NUM] = {0xff};

    //Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);
    
    //set username 0xff to delete user
   	if(0 != LIBIPMI_SetUser(&UDSSession, UserName, userid, DEFAULT_TIMEOUT ))
   	{
   		printf("Delete user fail!\n");
    	//Close session
    	LIBIPMI_CloseSession(&UDSSession );
    	websError(wp, 404, "Delete user fail!");    	
    	return;
   	}
  
    //Close session
    LIBIPMI_CloseSession(&UDSSession );

    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);
    cJSON_AddNumberToObject(data, "userid", userid);
   
    pStr = cJSON_PrintUnformatted(root);

//    printf("---> cJSON Str:\n%s\n", pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}


void checkLoginStatus(Webs *wp){
    char *loginStatus = (char*) websGetSessionVar(wp, "loginStatus", 0);
    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);
    cJSON_AddStringToObject(data, "loginStatus", loginStatus);
    pStr = cJSON_PrintUnformatted(root);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}

void logout(Webs *wp){
    websSetSessionVar(wp, "loginStatus", "failed");
    char *loginStatus = (char*) websGetSessionVar(wp, "loginStatus", 0);
    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);
    cJSON_AddStringToObject(data, "loginStatus", loginStatus);
    printf("---> come Str:\n");
    pStr = cJSON_PrintUnformatted(root);

    printf("---> cJSON Str:\n%s\n", pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}
