package fr.lithie.matchup.database;

import java.sql.ResultSet;
import java.sql.SQLException;

import fr.lithie.matchup.entities.Administrator;
import fr.lithie.matchup.entities.Role;
import fr.lithie.matchup.entities.Validity;
import fr.lithie.matchup.entities.base.BaseEntity;

public class AdministratorDAO extends UserDAO {
	
	public static final String TABLE ="administrator";
	public static final String ID ="id_admin";
	public static final String LASTNAME = "lastname_admin";
	public static final String FIRSTNAME = "firstname_admin";
	public static final String MAIL = "mail_admin";
	public static final String PHONE = "phone_admin";
	public static final String AVATAR = "logo_admin";
	public static final String ROLE = "role_admin";
	public static final String LOGIN = "login_admin";
	public static final String PASSWORD = "password_admin";
	public static final String VALID = "valid";
	
	
	public AdministratorDAO() {
		super(TABLE, ID);
		// TODO Auto-generated constructor stub
	}

	@Override
	public BaseEntity parseToObject(ResultSet resultSet) {
		Administrator admin = new Administrator();
		
		try {
			admin.setId(resultSet.getDouble(ID));
			admin.setLastname(resultSet.getString(LASTNAME));
			admin.setFirstname(resultSet.getString(FIRSTNAME));
			admin.setEmail(resultSet.getString(MAIL));
			admin.setPhone(resultSet.getString(PHONE));
			admin.setAvatar(resultSet.getBlob(AVATAR));
			if (resultSet.getString(ROLE).equals("admin")) {
				admin.setRole(Role.ADMIN);
			} else {
				admin.setRole(Role.valueOf(resultSet.getString(ROLE)));
			}
			admin.setLogin(resultSet.getString(LOGIN));
			admin.setPassword(resultSet.getString(PASSWORD));
			admin.setName(admin.getFirstname() + " " + admin.getLastname());
			admin.setValid(Validity.valueOf(resultSet.getString(VALID)));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			admin = null;
		}
		
		return admin;
	}

	@Override
	public String parseToString(BaseEntity item) {
		String res = "null,";
		Administrator admin = (Administrator) item;
		
		res += "'"+admin.getLastname()+"',";
		res += "'"+admin.getFirstname()+"',";
		res += "'"+admin.getEmail()+"',";
		res += "'"+admin.getPhone()+"',";
		res += "'"+admin.getRole()+"',";
		res += "'"+admin.getLogin()+"',";
		res += "'"+admin.getPassword()+"',";
		res += "'"+admin.getValid()+"'";

		return res;
	}

	@Override
	public String parseUpdateToString(BaseEntity item) {
		String res = "";
		Administrator admin = (Administrator) item;
		
		res += FIRSTNAME + " = '"+admin.getFirstname()+"',";
		res += LASTNAME + " = '"+admin.getLastname()+"',";
		res += MAIL + " = '"+admin.getEmail()+"',";
		res += PHONE + " = '"+admin.getPhone()+"',";
		res += ROLE + " = '"+admin.getRole()+"',";
		res += LOGIN + " = '"+admin.getLogin()+"',";
		res += PASSWORD + " = '"+admin.getPassword()+"',";
		res += VALID + " = '"+admin.getValid()+"'";

		return res;
	}
	
	
}

@@ -410,22 +410,16 @@
  ID =%22id
-_admin
 %22;%0A%09publ
@@ -457,30 +457,24 @@
  = %22lastname
-_admin
 %22;%0A%09public s
@@ -514,22 +514,16 @@
 irstname
-_admin
 %22;%0A%09publ
@@ -557,22 +557,16 @@
  = %22mail
-_admin
 %22;%0A%09publ
@@ -602,22 +602,16 @@
 = %22phone
-_admin
 %22;%0A%09publ
@@ -647,224 +647,54 @@
  = %22
-logo_admin%22;%0A%09public static final String ROLE = %22role_admin%22;%0A%09public static final String LOGIN = %22login_admin%22;%0A%09public static final String PASSWORD = %22password_admin%22;%0A%09public static final String VALID = %22valid%22;%0A%09
+image%22;%0A%09private UserDAO uDao = new UserDAO();
 %0A%09%0A%09
@@ -905,31 +905,84 @@
 ;%0A%09%09
-%0A%09%09try %7B%0A%09%09%09admin.setId
+UserDAO uDao = new UserDAO();%0A%09%09%0A%09%09try %7B%0A%09%09%09admin = (Administrator) uDao.get
 (res
@@ -1256,268 +1256,8 @@
 ));%0A
-%09%09%09if (resultSet.getString(ROLE).equals(%22admin%22)) %7B%0A%09%09%09%09admin.setRole(Role.ADMIN);%0A%09%09%09%7D else %7B%0A%09%09%09%09admin.setRole(Role.valueOf(resultSet.getString(ROLE)));%0A%09%09%09%7D%0A%09%09%09admin.setLogin(resultSet.getString(LOGIN));%0A%09%09%09admin.setPassword(resultSet.getString(PASSWORD));%0A
 %09%09%09a
@@ -1324,73 +1324,8 @@
 ));%0A
-%09%09%09admin.setValid(Validity.valueOf(resultSet.getString(VALID)));%0A
 %09%09%7D 
@@ -1518,32 +1518,8 @@
 ) %7B%0A
-%09%09String res = %22null,%22;%0A
 %09%09Ad
@@ -1551,32 +1551,139 @@
 nistrator) item;
+%0A%0A%09%09uDao.insert(admin);%0A%09%09//might need test to confirm user insertion?%0A%09%09String res = %22%22+admin.getId()+%22,%22;
 %0A%09%09%0A%09%09res += %22'%22
@@ -1835,140 +1835,12 @@
 += %22
-'%22+admin.getRole()+%22',%22;%0A%09%09res += %22'%22+admin.getLogin()+%22',%22;%0A%09%09res += %22'%22+admin.getPassword()+%22',%22;%0A%09%09res += %22'%22+admin.getValid()+%22'
+null
 %22;%0A%0A
@@ -1941,16 +1941,38 @@
 s = %22%22;%0A
+%09%09uDao.update(item);%0A%0A
 %09%09Admini
@@ -2007,24 +2007,26 @@
 or) item;%0A%09%09
+%09%09
 %0A%09%09res += FI
@@ -2228,12 +2228,14 @@
  += 
-ROLE
+AVATAR
  + %22
@@ -2253,159 +2253,14 @@
 .get
-Role()+%22',%22;%0A%09%09res += LOGIN + %22 = '%22+admin.getLogin()+%22',%22;%0A%09%09res += PASSWORD + %22 = '%22+admin.getPassword()+%22',%22;%0A%09%09res += VALID + %22 = '%22+admin.getValid
+Avatar
 ()+%22

Correct AdministratorDAO