//---------------------------------------------------------------------------

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "EncryptionF.h"
#pragma hdrstop

#include <ADODB.hpp>
#include <DB.hpp>
#include <winsock.h>

#include "FrmLoginF.h"
#include "FrmPswdChngeF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "dxGDIPlusClasses"
#pragma resource "*.dfm"

TFRMLogin *FRMLogin = NULL;
//---------------------------------------------------------------------------
__fastcall TFRMLogin::TFRMLogin(TComponent* Owner)
    : TForm(Owner)
{
    //ITSSkin_Load(this);

    m_nLoginTryCnt = 0;

    m_sUserName      = "";  //사용자 명
    m_sUserID        = "";  //사용자 ID
    m_sUserRightID   = "";  //사용자 권한 ID
    m_sUserRightName = "";  //사용자 권한 명
    m_sLoginTime     = "";

    m_bLogin         = false;

    LbMessage->Caption = "";
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::BtnCloseClick(TObject *Sender)
{
    ModalResult = mrCancel;
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::BtnPswdChngeClick(TObject *Sender)
{
    try
    {
        FRMPswdChnge = new TFRMPswdChnge(this);

        FRMPswdChnge->EdUserID->Text = EdUserID->Text;

        FRMPswdChnge->ShowModal();

        EdUserID->Text = FRMPswdChnge->EdUserID->Text.Trim();
        EdPswd->Text   = FRMPswdChnge->EdCnfmNewPswd->Text.Trim();

        delete FRMPswdChnge;
        FRMPswdChnge = NULL;
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::BtnLoginClick(TObject *Sender)
{
    String sUserID   = Trim(EdUserID->Text);
    String sUserPswd = Trim(EdPswd->Text);

    //c4ca4238a0b923820dcc509a6f75849b  == "1"
    if (sUserID.Compare("") == 0)
    {
        Application->NormalizeTopMosts();
        if (g_AppCfg.sLang == "kr") Application->MessageBox(L"아이디를 입력하세요.", L"아이디 입력 오류", MB_OK|MB_ICONERROR);     // ID를 입력하세요.
        else                        Application->MessageBox(L"Please enter Use ID.", L"User ID Error", MB_OK|MB_ICONERROR);     // ID를 입력하세요.
        Application->RestoreTopMosts();
        EdUserID->SetFocus();
        return;
    }
    if (sUserPswd.Compare("") == 0)
    {
        Application->NormalizeTopMosts();
        if (g_AppCfg.sLang == "kr") Application->MessageBox(L"비밀번호를 입력하세요.", L"비밀번호 입력 오류", MB_OK|MB_ICONERROR);     // 비밀번호를 입력하세요.
        else                        Application->MessageBox(L"Please enter User Password.", L"Password Error", MB_OK|MB_ICONERROR);     // 비밀번호를 입력하세요.
        Application->RestoreTopMosts();
        EdPswd->SetFocus();
        return;
    }

    int nRes = GetUserPassword(sUserID, sUserPswd);
    if (LOGIN_DB_ERR == nRes)
    {
        return;
    }
    if (LOGIN_OK == nRes)
    {
        m_bLogin = true;
        ModalResult = mrOk;
        //Close();
    }
    else
    {
        String sErrMsg;
        m_bLogin = false;
        if (LOGIN_ID_ERR == nRes)
        {
            if (g_AppCfg.sLang == "kr") sErrMsg = "등록되지 않은 사용자 입니다.";
            else                        sErrMsg = "Unregistered user.";//"등록되지 않은 사용자 입니다.";
            Application->NormalizeTopMosts();
            if (g_AppCfg.sLang == "kr") Application->MessageBox(sErrMsg.c_str(), L"로그인 오류", MB_OK|MB_ICONERROR);     // 등록되지 않은 사용자 입니다.
            else                        Application->MessageBox(sErrMsg.c_str(), L"Login Error", MB_OK|MB_ICONERROR);     // 등록되지 않은 사용자 입니다.
            Application->RestoreTopMosts();
            EdUserID->SetFocus();
        }
        if (LOGIN_PSWD_ERR == nRes)
        {
            if (g_AppCfg.sLang == "kr") sErrMsg = "비밀번호가 정확하지 않습니다.";
            else                        sErrMsg = "The password is incorrect.";//"비밀번호가 정확하지 않습니다.";
            Application->NormalizeTopMosts();
            if (g_AppCfg.sLang == "kr") Application->MessageBox(sErrMsg.c_str(), L"로그인 오류", MB_OK|MB_ICONERROR);     // 비밀번호가 정확하지 않습니다.
            else                        Application->MessageBox(sErrMsg.c_str(), L"Login Error", MB_OK|MB_ICONERROR);     // 비밀번호가 정확하지 않습니다.
            Application->RestoreTopMosts();
            EdPswd->SetFocus();
        }
        
        m_nLoginTryCnt++;
        if (g_AppCfg.sLang == "kr") LbMessage->Caption = sErrMsg + "\r\n접속실패 3회면 프로그램이 종료됩니다.\r\n현재 "+String(m_nLoginTryCnt) +" 회";
        else                        LbMessage->Caption = sErrMsg + "\r\nIf the connection fails 3 times, the program ends.\r\nCurrent "+String(m_nLoginTryCnt) +" Trys";
        if (m_nLoginTryCnt >= USER_LOGIN_TRY_CNT)
        {
            Application->NormalizeTopMosts();
            if (g_AppCfg.sLang == "kr") Application->MessageBox(L"사용자 로그인 오류 횟수(3회) 초과 오류입니다.", L"로그인 오류", MB_OK|MB_ICONERROR);     // 사용자 인증오류입니다. 프로그램을 종료합니다.
            else                        Application->MessageBox(L"Error exceeding user login failure count (3).", L"Login Error", MB_OK|MB_ICONERROR);     // 사용자 인증오류입니다. 프로그램을 종료합니다.
            Application->RestoreTopMosts();

            ModalResult = mrCancel;
            //Close();
        }
    }
}
//---------------------------------------------------------------------------

int __fastcall TFRMLogin::GetUserPassword(String sUserID, String sPswd)
{
    int    nRes;
    String sTmpPswd;
    String sEncPswd;
    String sQry;


    nRes     = LOGIN_OK;
    sTmpPswd = "";


    if (!ITSDb_IsOpen())
    {
        if (!ITSDb_Open())
        {
            Application->NormalizeTopMosts();
            if (g_AppCfg.sLang == "kr") Application->MessageBox(L"데이터베이스에 연결할 수 없습니다.", L"데이터베이스 연결 오류", MB_OK|MB_ICONERROR);     // 비밀번호를 입력하세요.
            else                        Application->MessageBox(L"Unable to connect to database.", L"Database Connect Error", MB_OK|MB_ICONERROR);     // 비밀번호를 입력하세요.
            Application->RestoreTopMosts();
            EdPswd->SetFocus();
            return LOGIN_DB_ERR;
        }
    }
    if (!ITSDb_IsOpen())
    {
        return LOGIN_DB_ERR;
    }

    sEncPswd = sPswd;

    sQry = "SELECT USERID   AS USER_ID,                                  \r\n"
           "       USERNAME AS NAME,                                     \r\n"
           "       DEGREE   AS LEVELNAME,                                \r\n"
           "       crypto_decrypt(PASSWORD)             AS PASSWORDNAME, \r\n"
           "       TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS LOGINTIME     \r\n"
           "  FROM USER_INFO                                             \r\n"
           " WHERE USERID = :p01                                         \r\n";

    TADOQuery *adoQry = NULL;
    try
    {
        try
        {
            adoQry = new TADOQuery(NULL);
            adoQry->Connection = ITSDb_GetConnection();
            adoQry->Close();
            adoQry->SQL->Text = sQry;
            adoQry->Parameters->ParamByName("p01")->Value = sUserID;
            adoQry->Open();
            if (!adoQry->Eof)
            {
                adoQry->First();
                sTmpPswd = adoQry->FieldByName("passwordname")->AsString;

                if (sTmpPswd != sEncPswd)
                {
                    nRes = LOGIN_PSWD_ERR;
                }
                else
                {
                    m_sUserID        = adoQry->FieldByName("USER_ID")->AsString;  
                    m_sUserName      = adoQry->FieldByName("NAME")->AsString;
                    m_sUserRightID   = adoQry->FieldByName("levelname")->AsString;
                    m_sUserRightName = adoQry->FieldByName("levelname")->AsString;
                    m_sLoginTime     = adoQry->FieldByName("logintime")->AsString;
                }
            }
            else
            {
                nRes = LOGIN_ID_ERR;
            }
            adoQry->Close();
        }
        catch(EDatabaseError &E)
        {
            nRes = LOGIN_DB_ERR;
            throw Exception(String(E.ClassName())+E.Message);
        }
        catch (Exception &exception)
        {
            nRes = LOGIN_DB_ERR;
            throw Exception(String(exception.ClassName())+exception.Message);
        }
        catch(...)
        {
            nRes = LOGIN_DB_ERR;
            if (g_AppCfg.sLang == "kr")  throw Exception("알수없는 오류가 발생하였습니다.");
            else                         throw Exception("Unknown Error.");
        }
    }
    __finally
    {
        if (adoQry)
        {
            adoQry->Close();
            delete adoQry;
        }
    }

    return nRes;
}
//---------------------------------------------------------------------------

String __fastcall TFRMLogin::GetLocalIp()
{
    char slocal[256];
    WSAData wsaData;


    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
        return "";


    if (gethostname(slocal, 256) == SOCKET_ERROR)
        return "";

    hostent *hos = gethostbyname(slocal);
    if (hos == NULL)
        return "";

    char *addr = *(hos->h_addr_list);

    String LocalIP = String((unsigned char)addr[0]) + '.'
                   + String((unsigned char)addr[1]) + '.'
                   + String((unsigned char)addr[2]) + '.'
                   + String((unsigned char)addr[3]);
    WSACleanup();
    return LocalIP;
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::FormShow(TObject *Sender)
{
    if (EdUserID->Text != "")
    {
        EdPswd->SetFocus();
    }
    else
    {
        EdUserID->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::EdUserIDKeyPress(TObject *Sender, char &Key)
{
    if (Key == 13)
    {
        EdPswd->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::EdPswdKeyPress(TObject *Sender, char &Key)
{
    if (Key == 13)
    {
        BtnLoginClick(BtnLogin);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::FormCreate(TObject *Sender)
{
    /*
    *  스킨 변경
    */
    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

	SetLocalSkin();
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::SetLocalSkin()
{
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
//---------------------------------------------------------------------------

void __fastcall TFRMLogin::FormClose(TObject *Sender, TCloseAction &Action)
{
    //Action = caFree;
}
//---------------------------------------------------------------------------

