#include "common_tool.h"
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "mermsemr_api.h"
#include "safestring.h"
#include "cfg.h"
#include <string>
#include "pgsql_wrapper.h"
#include "pgsql.h"

//char confirmation[20] = "";
//Confirmation(payment_id, confirmation, sizeof (confirmation));

void GenerateInnerConfirmationSeed(char * number, int sz, int seed) {
    char chars[] = "WRB01234WRB56789WRB";
    int n = sizeof (chars);
    int i;
    /*
            time_t t;
            time( &t );
            pid_t pid = getpid();

            srand(t+pid);
     */
    srand((unsigned int) time((time_t *) NULL));

    for (i = 0; i < 10 && i < sz - 1; i++) {
        int k = seed % n;
        if (seed > 1 && k == i) {
            number[i] = chars[n - k];
        } else {
            number[i] = chars[ (int) ((n - 1.0) * rand() / (RAND_MAX + 1.0)) ];
        }
    }
    number[i] = 0;
}

void ConfirmationNew(unsigned long payment_id, char * number, int sz) {
    SQLINTEGER rows;
    char confirmation[11];

    // Lock table to prevent generation of two identical confirmation numbers
    pgsql_exec("BEGIN; LOCK TABLE members_payments;");
    int i = 0;
    do {
        GenerateInnerConfirmationSeed(confirmation, sizeof (confirmation), i++);

        //st.CloseCursor();
        rows = 0;
        const PGresult *res = pgsql_query("SELECT id FROM members_payments WHERE confirmation='%s'", confirmation);
        if (res != NULL) {
            rows = pgsql_num_rows(res);
        }
    } while (rows > 0 && i < 100);

    pgsql_exec("UPDATE members_payments SET confirmation='%s', dt_confirmed=now() WHERE id=%lu", confirmation, payment_id);
    pgsql_exec("COMMIT"); // Unlock table 'data'
    strsafecpy(number, confirmation, sz);
}

void GetMemberAccountLink2(long member_id, unsigned long acc_link, char * number);
long GetMemberAccountLink(long member_id, CVars &out){

long ret=0;
char acc_link[11];

 ret = load_db_record(out, "SELECT acc_link FROM members WHERE id =  %lu",member_id);
 if ( out["acc_link"]== "" ){ 
 GetMemberAccountLink2( member_id, sizeof (acc_link), acc_link);
 load_db_record(out, "SELECT acc_link FROM members WHERE id =  %lu",member_id);// try again
 }
 else
 {
	
 }
 

return 0;
}

void GetMemberAccountLink2(long member_id, unsigned long acc_link, char * number) {
    SQLINTEGER rows;
    char offer_code[14];

    // Lock table to prevent generation of two identical confirmation numbers
    pgsql_exec("BEGIN; LOCK TABLE members;");
    int i = 0;
    do {
        GenerateInnerConfirmationSeed(offer_code, sizeof (offer_code), i++);

        //st.CloseCursor();
        rows = 0;
        const PGresult *res = pgsql_query("SELECT id FROM members WHERE  acc_link='%s'", offer_code);
        if (res != NULL) {
            rows = pgsql_num_rows(res);
        }
    } while (rows > 0 && i < 100);

    pgsql_exec("UPDATE members SET acc_link='%s' WHERE id=%lu", offer_code, member_id);
    pgsql_exec("COMMIT"); // Unlock table 'data'
    //	strsafecpy( acc_link, offer_code, sz );
}
