/**
 * 
 */
package org.mynonglam.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mynonglam.connectdb.ConnectionDTB;
import org.mynonglam.entitys.Account;
import org.mynonglam.entitys.Album;
import org.mynonglam.entitys.CommentPhoto;
import org.mynonglam.entitys.LinkPhoto;
import org.mynonglam.entitys.ShareType;

/**
 * @author Dao
 *
 */
public class AlbumDAO {

	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;
	
	/**
	 * 
	 */
	public AlbumDAO() {
		connection = ConnectionDTB.getConnection();
	}
	
	public boolean insertAlbum(Album album) {
		boolean result = false;
		
		String sql = "Insert INTO album(idalbum, email, name, datecreate, description, idshare) VALUES(?,?,?,?,?,'1')";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, album.getIdalbum());
			statement.setString(2, album.getAccount().getEmail());
			statement.setString(3, album.getName());
			statement.setString(4, album.getDatecreate());
			statement.setString(5, album.getDescription());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean insertLinkPhoto(LinkPhoto linkPhoto) {
		boolean result = false;
		
		String sql = "Insert INTO linkphotos(idphoto, idalbum, link, discription, datecreate) VALUES (?,?,?,?,?)";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, linkPhoto.getIdphoto());
			statement.setString(2, linkPhoto.getAlbum().getIdalbum());
			statement.setString(3, linkPhoto.getLink());
			statement.setString(4, linkPhoto.getDiscription());
			statement.setString(5, linkPhoto.getDatecreate());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public List<Album> getAlbumsView(String idacc) {
		List<Album> result = new ArrayList<Album>();
		
		String sql = "SELECT idalbum, name, datecreate, idshare FROM album where email=? ORDER BY name";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idacc);
			
			ResultSet resultSet = statement.executeQuery();
			while(resultSet.next()) {
				System.out.println("saslkja");
				Album temp = new Album();
				temp.setIdalbum(resultSet.getString("idalbum"));
				temp.setName(resultSet.getString("name"));
				temp.setDatecreate(resultSet.getString("datecreate"));
				ShareType shareType = new ShareType();
				shareType.setIdshare(resultSet.getString("idshare"));
				temp.setSharetype(shareType);
				
				result.add(temp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		for(Album album : result) {
			album.setDescription(getOneImageAlbum(album.getIdalbum()));
		}
		//System.out.println(result.size());
		return result;
	}
	
	public Album getAlbumByID(String idalbum) {
		Album result = null;
		
		String sql = "SELECT idalbum, name, datecreate, description, idshare FROM album where idalbum=?";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idalbum);
			
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = new Album();
				result.setIdalbum(idalbum);
				result.setName(resultSet.getString("name"));
				result.setDatecreate(resultSet.getString("datecreate"));
				ShareType shareType = new ShareType();
				shareType.setIdshare(resultSet.getString("idshare"));
				result.setSharetype(shareType);
				result.setDescription(getOneImageAlbum(resultSet.getString("description")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getOneImageAlbum(String idalbum) {
		String result = "images/default.jpg";
		String sql = "SELECT link FROM linkphotos where idalbum=? LIMIT 0,1";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idalbum);
			
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = resultSet.getString("link");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public List<LinkPhoto> getAlbumDetailsView(String idalbum, int start, int end) {
		List<LinkPhoto> result = new ArrayList<LinkPhoto>();
		
		String sql = "SELECT idphoto, link, discription, datecreate FROM linkphotos where idalbum=? ORDER BY datecreate DESC LIMIT ?,?";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idalbum);
			statement.setInt(2, start);
			statement.setInt(3, end);
			
			ResultSet resultSet = statement.executeQuery();
			while (resultSet.next()) {
				LinkPhoto temp = new LinkPhoto();
				temp.setIdphoto(resultSet.getString("idphoto"));
				temp.setLink(resultSet.getString("link"));
				temp.setDiscription(resultSet.getString("discription"));
				temp.setDatecreate(resultSet.getString("datecreate"));
				result.add(temp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public List<CommentPhoto> getCommentPhotoView(String idphoto) {
		List<CommentPhoto> result = new ArrayList<CommentPhoto>();
		
		String sql = "SELECT idcomphoto, email, content, commentdate FROM commentphoto where idphoto=? ORDER BY idphoto DESC";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idphoto);
			
			ResultSet resultSet = statement.executeQuery();
			
			while (resultSet.next()) {
				CommentPhoto comment = new CommentPhoto();
				comment.setIdcomphoto(resultSet.getString("idcomphoto"));
				comment.setContent(resultSet.getString("content"));
				comment.setCommentdate(resultSet.getString("commentdate"));
				Account account = new AccountDAO().getAccount(resultSet.getString("email"));
				comment.setAccount(account);
				result.add(comment);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public LinkPhoto getLinkPhoto(String idphoto) {
		LinkPhoto result = null;
		String sql = "SELECT idphoto, link, discription, datecreate FROM linkphotos where idphoto=? ORDER BY datecreate DESC";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idphoto);
			
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result =  new LinkPhoto();
				result.setIdphoto(resultSet.getString("idphoto"));
				result.setLink(resultSet.getString("link"));
				result.setDiscription(resultSet.getString("discription"));
				result.setDatecreate(resultSet.getString("datecreate"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDAlbumLast() {
		String result = "";
		
		String sql = "SELECT idalbum FROM album ORDER BY idalbum DESC LIMIT 0,1";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("idalbum");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDLinkPhotoLast() {
		String result = "";
		
		String sql = "SELECT idphoto FROM linkphotos ORDER BY idphoto DESC LIMIT 0,1";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("idphoto");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static void main(String[] args) {
		System.out.println(new AlbumDAO().getAlbumsView("dlduynam@gmail.com").size());
	}

}
