#include "stdafx.h"
#include "ChatSet.h"
#include "DataProcess.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

///////////////////////////////////////////////////
enum {Has_Login, Wrong_Password,None,Login_Success};
enum {Error,Has_Reg,Reg_Success};
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
BOOL operator==(const ChatRoom &ch1,const ChatRoom &ch2)
{
		if(ch2.GetName()=="")
		   return (ch1.GetOwner()==ch2.GetOwner()) ? true : false;
	   else if(ch2.GetOwner()=="")
		   return (ch1.GetName()==ch2.GetName()) ? true : false;
	   else
		   return ((ch1.GetName()==ch2.GetName())&&(ch1.GetOwner()==ch2.GetOwner())) ? true : false;
}

BOOL operator==(const Ro_Me &index1, const Ro_Me &index2)
{
	   if(index2._parti_name=="")
		   return (index1._room_name==index2._room_name) ? true : false;
	   else if(index2._room_name=="")
		   return (index1._parti_name==index2._parti_name) ? true : false;
	   else
		   return ((index1._room_name==index2._room_name)&&(index1._parti_name==index2._parti_name)) ? true : false;
}

bool ChatRoom_DB::OnNamequery(CString name)
{
	try
	{
		if(name=="") return false;
		if(dd.IsOpen())
		{	
			m_pset->Open(CRecordset::snapshot);
			m_pset->m_strFilter=CString("ID_Name=\'")+name+"\'";
			m_pset->Requery();
			if(m_pset->IsEOF()) 
				return false;
			else
				return true;
		}
	}
	catch(CDBException *p)
	{
		AfxMessageBox(p->m_strError);
		return false;
	}
	return false;
}

int ChatRoom_DB::Login(CString name,CString PWD)
{
	int state=None;
	if(OnNamequery(name))
	{
		state=Has_Login;
		if(!(m_pset->m_OnLine)) 
		{
			state=Wrong_Password;
			if(m_pset->m_Password==PWD)
			{
				m_pset->Edit();
				m_pset->m_OnLine=TRUE;
				m_pset->Update();
				state=Login_Success;
			}
		}
	}
	m_pset->Close();
	return state;
}

CPerson ChatRoom_DB::SearchName(CString name)
{
	CPerson person1;
	if(OnNamequery(name))
	{
		person1.idname=m_pset->m_IDName;
		person1.addr=m_pset->m_Address;
		person1.ipadd=m_pset->m_IPAddr;
		person1.nickname=m_pset->m_NickName;
		person1.email=m_pset->m_Email;
		person1.sex=m_pset->m_Sex;
		person1.tel=m_pset->m_Tel;
		person1.user_color=m_pset->m_Color;
		if(m_pset->m_OnLine==TRUE)
			person1.state="ONLINE";
		else  person1.state="OFFLINE";
	}
	else
	{
		person1.idname="";
	}
	m_pset->Close();
	return person1;
}

bool ChatRoom_DB::Logout(CString name)
{
	bool check=false;
	if(OnNamequery(name))
	{  	
	   if((m_pset->m_OnLine))
		{
			m_pset->Edit();
			m_pset->m_OnLine=FALSE;
			m_pset->Update();
			check=true;
		}	

	}
	m_pset->Close();
	return check;
}

bool ChatRoom_DB::EditData_PWD(CString name,CString pwd)
{
	bool Failure=false;
	if(OnNamequery(name))
	{
		if(m_pset->m_OnLine)
		{
			m_pset->Edit();
			m_pset->m_Password=pwd;
			m_pset->Update();
			Failure=true;
		}
	}
	m_pset->Close();
	return Failure;
}

bool ChatRoom_DB::EditData_Email(CString name,CString mail)
{
	bool Failure=false;
	if(OnNamequery(name))
	{
		if(m_pset->m_OnLine)
		{
			m_pset->Edit();
			m_pset->m_Email=mail;
			m_pset->Update();
			Failure=true;
		}
	}
	m_pset->Close();
	return Failure;
}

bool ChatRoom_DB::EditData_Nick(CString name,CString niname)
{
	bool Failure=false;
	if(OnNamequery(name))
	{
		if(m_pset->m_OnLine)
		{
			m_pset->Edit();
			m_pset->m_NickName=niname;
			m_pset->Update();
			Failure=true;
		}
	}
	m_pset->Close();
	return Failure;
}

bool ChatRoom_DB::EditData_TEL(CString name,CString tel)
{
	bool Failure=false;
	if(OnNamequery(name))
	{
		if(m_pset->m_OnLine)
		{
			m_pset->Edit();
			m_pset->m_Tel=tel;
			m_pset->Update();
			Failure=true;
		}
	}

	m_pset->Close();
	return Failure;
}

bool ChatRoom_DB::EditData_Addr(CString name,CString addr)
{
	bool Failure=false;
	if(OnNamequery(name))
	{
		if(m_pset->m_OnLine)
		{
			m_pset->Edit();
			m_pset->m_Address=addr;
			m_pset->Update();
			Failure=true;
		}
	}

	m_pset->Close();
	return Failure;
}

bool ChatRoom_DB::EditData_Color(CString name,CString color)
{
	bool Failure=false;
	if(OnNamequery(name))
	{
		if(m_pset->m_OnLine)
		{
			m_pset->Edit();
			m_pset->m_Color=color;
			m_pset->Update();
			Failure=true;
		}
	}

	m_pset->Close();
	return Failure;
}

int ChatRoom_DB::SavetoDB(CString name,CString ipaddr,CString psd,
				CString nickname,CString sex,CString addr,CString email,
				CString tel,CString color)
{
	int state=Has_Reg;
	if(!OnNamequery(name))
	{
	CDatabase db;
	try{
		if(db.Open(_T("ODBC;DSN=Member")))
		{
			CString strSQL="INSERT INTO [Table] VALUES('"
				+name+"','"+psd+"',1,'"+ipaddr+"','"+nickname+"','"+sex+"','"+tel+"','"
				+addr+"','"+email+"','"+color+"')";
			db.ExecuteSQL(strSQL);
			state=Reg_Success;
			db.Close();
		}
	}
	catch(CDBException *p)
	{
		AfxMessageBox(p->m_strError);
		state=Error;
		db.Close();
	}
	}
	m_pset->Close();
	return state;
}

void ChatRoom_DB::Del(CString name)
{
	if(OnNamequery(name))
	{
		if(m_pset->m_OnLine)
		{
			m_pset->Delete();
			m_pset->MoveNext();
		}
	}

	m_pset->Close();
}

void ChatRoom_DB::OnInitial()
{
	dd.Open(_T("ODBC;DSN=Member"));
	m_pset=new CChatSet(&dd);

}

void ChatRoom_DB::PersonOut(CString p1)
{	
	//找出這個人所擁有的所有聊天室
	POSITION pos = Chatroomvec.GetHeadPosition();
		for(;;)
		{
			pos=Chatroomvec.Find(ChatRoom("",p1));
			if(pos!=NULL)//聊天室擁有者
			{
				/*//清除聊天室以及成員紀錄
				POSITION pos2=Room_Memberset.GetHeadPosition();
				for(;;) 
				{
					pos2=Room_Memberset.Find(Ro_Me(Chatroomvec.GetAt(pos).GetName(),""));
				    if(pos2!=NULL) Room_Memberset.RemoveAt(pos2);
					else break;
				}
				//移除這個人所擁有的所有聊天室
				Chatroomvec.RemoveAt(pos);*/
				Close_Chat(Chatroomvec.GetAt(pos).GetName(),p1);
			}
			else break;
		}
		
		//清除所參與的聊天室紀錄
		POSITION pos2=Room_Memberset.GetHeadPosition();
		for(;;) 
		{
			pos2=Room_Memberset.Find(Ro_Me("",p1));
			if(pos2!=NULL) Room_Memberset.RemoveAt(pos2);
			else break;
		}

}

POSITION ChatRoom_DB::WhereIs(CString user)
{
	POSITION pos;
	return pos=Room_Memberset.Find(Ro_Me("",user));
}

bool ChatRoom_DB::Open_New(CString name,CString ch1)
{
	bool check=false;
	if(OnNamequery(name))
	{
		if (m_pset->m_OnLine)
		{
			POSITION pos=Chatroomvec.Find(ChatRoom(ch1,""));
			if(pos==NULL)//如果沒有這個包廂的話
			{
				Chatroomvec.AddTail(ChatRoom(ch1,name));
				Room_Memberset.AddTail(Ro_Me(ch1,name));
				check=true;
			}
		}
	}
	m_pset->Close();
	return check;
}


bool ChatRoom_DB::Close_Chat(CString name,CString ch1)
{
	bool check=false;
 	if(OnNamequery(name))
	{
			POSITION pos=Chatroomvec.Find(ChatRoom(ch1,name));
			if(pos!=NULL)//如果有這個包廂的話
			{
				POSITION pos2=Room_Memberset.GetHeadPosition();
				for(;;) 
				{
					pos2=Room_Memberset.Find(Ro_Me(Chatroomvec.GetAt(pos).GetName(),""));
				    if(pos2!=NULL) Room_Memberset.RemoveAt(pos2);
					else break;
				}
				Chatroomvec.RemoveAt(pos);
				check=true;
			}
	}
	m_pset->Close();
	return check;
}

bool ChatRoom_DB::JOIN_Chat(CString name,CString ch1)
{
	bool check=false;
 	if(OnNamequery(name))
	{
		if (m_pset->m_OnLine)
		{   
			ChatRoom room=ChatRoom(ch1,"");
			POSITION pos=Chatroomvec.Find(ChatRoom(ch1,""));
			if(pos!=NULL)//如果有這個包廂的話
			{
				POSITION pos2=Room_Memberset.Find(Ro_Me(Chatroomvec.GetAt(pos).GetName(),name));
				if(pos2==NULL)
				{
					Room_Memberset.AddTail(Ro_Me(Chatroomvec.GetAt(pos).GetName(),name));
					check=true;
				}
			}
		}
	}
	m_pset->Close();
	return check;
}

int ChatRoom_DB::Left_Chat(CString name,CString ch1)
{
	int check=0;
 	if(OnNamequery(name))
	{
		//if (m_pset->m_OnLine)
		//{  
			ChatRoom room=ChatRoom(ch1,"");
			POSITION pos=Chatroomvec.Find(ChatRoom(ch1,""));
			if(pos!=NULL)//如果有這個包廂的話
			{
				if(Chatroomvec.GetAt(pos).GetOwner()!=name)
				{
					POSITION pos2=Room_Memberset.Find(Ro_Me(Chatroomvec.GetAt(pos).GetName(),name));
					if(pos2!=NULL) 
					{ 
						Room_Memberset.RemoveAt(pos2);
						check=2;
					}
				}
				else 
					check=1;

			}
		//}
	}
	m_pset->Close();
	return check;
}

bool ChatRoom_DB::Get_Chat_List(CString chat1,CStringList &vec)
{
	vec.RemoveAll();
	ChatRoom room=ChatRoom(chat1,"");
	POSITION pos=Chatroomvec.Find(ChatRoom(chat1,""));
	if(pos!=NULL)//如果有這個包廂的話
	{
		for(POSITION pos2=Room_Memberset.GetHeadPosition();pos2!=NULL;) 
		{
			if (Room_Memberset.GetAt(pos2)._room_name==chat1)
				vec.AddTail(Room_Memberset.GetNext(pos2)._parti_name);
			else
				Room_Memberset.GetNext(pos2);

		}
		return true;
	}
	return false;
}

void ChatRoom_DB::Owner_Get_List(CString name,CStringList &vec)
{
	vec.RemoveAll();
 	if(OnNamequery(name))
	{
		if (m_pset->m_OnLine)
		{
			ChatRoom room=ChatRoom("",name);
			for(POSITION pos=Chatroomvec.GetHeadPosition();pos!=NULL;) 
			{
				if (Chatroomvec.GetAt(pos).GetOwner()==name)
				vec.AddTail(Chatroomvec.GetNext(pos).GetName());
			}
		}
	}
	m_pset->Close();
}

void ChatRoom_DB::GetAllChatroom(CStringList &vec)
{
	vec.RemoveAll();
	for (POSITION pos=Chatroomvec.GetHeadPosition();pos!=NULL;)
		vec.AddTail(Chatroomvec.GetNext(pos).GetName());
}
