package com.github.assisstion.MTGCardPortfolio;

import java.awt.Component;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.swing.JOptionPane;

import com.github.assisstion.Shared.Settings;

public abstract class AbstractPlatform implements Platform{

	protected PortfolioFrame pf;
	protected CardDatabaseTableModel tableModel;
	protected List<CardData> cardDatabaseData;
	protected HashSet<String> expansionSet;
	protected List<String> visibleColumns;
	protected List<CardDatabaseTableModel> providedModels;
	protected CardImageCacher cic;
	protected boolean commitMode;
	protected boolean enableSettings;
	protected boolean unsavedChanges;
	protected String fileLocation;
	protected String sortColumn;
	protected boolean sortDescending;

	public AbstractPlatform(){

	}

	public AbstractPlatform(boolean commitMode){
		this.commitMode = commitMode;
	}

	@Override
	public Platform getPlatform(){
		return this;
	}

	@Override
	public PortfolioFrame getPortfolioFrame(){
		return pf;
	}

	@Override
	public CardDatabaseTableModel getTableModel(){
		return tableModel;
	}

	@Override
	public List<CardData> getCardData(){
		return cardDatabaseData;
	}

	@Override
	public HashSet<String> getExpansions(){
		return expansionSet;
	}

	@Override
	public CardImageCacher getCardImageCacher(){
		return cic;
	}

	@Override
	public boolean hasUnsavedChanges(){
		return unsavedChanges;
	}

	@Override
	public void setSettingsEnabled(boolean enabled){
		enableSettings = enabled;
	}

	@Override
	public boolean isSettingsEnabled(){
		return enableSettings;
	}

	@Override
	public Properties getSettings(){
		return Settings.getSettings("PortfolioPreferences");
	}

	@Override
	public void setSort(String column, boolean descending){
		sortColumn = column;
		sortDescending = descending;
		reSort();
	}

	@Override
	public void setSortColumn(String column){
		sortColumn = column;
	}

	@Override
	public void setSortDescending(boolean descending){
		sortDescending = descending;
	}

	@Override
	public String getSortColumn(){
		return sortColumn;
	}

	@Override
	public boolean getSortDescending(){
		return sortDescending;
	}

	@Override
	public void reSort(){
		if(providedModels == null){
			providedModels = new LinkedList<CardDatabaseTableModel>();
		}
		for(CardDatabaseTableModel cdtm : providedModels){
			cdtm.sortData(sortColumn, sortDescending);
		}
	}

	@Override
	public String getGlobalFileLocation(){
		if(fileLocation == null){
			if(pf == null){
				return null;
			}
			return pf.getFileLocation();
		}
		return fileLocation;
	}

	@Override
	public void setGlobalFileLocation(String fileLocation){
		this.fileLocation = fileLocation;
	}

	@Override
	public void setVisibleColumns(List<String> visibleColumns){
		this.visibleColumns = visibleColumns;
		if(providedModels == null){
			providedModels = new LinkedList<CardDatabaseTableModel>();
		}
		for(CardDatabaseTableModel cdtm : providedModels){
			cdtm.setVisibleColumns(visibleColumns);
		}
	}

	@Override
	public List<String> getVisibleColumns(){
		return visibleColumns;
	}

	@Override
	public void addProvidedModel(CardDatabaseTableModel cdtm){
		if(providedModels == null){
			providedModels = new LinkedList<CardDatabaseTableModel>();
		}
		providedModels.add(cdtm);
	}

	@Override
	public boolean isCommitMode(){
		return commitMode;
	}

	@Override
	public boolean saveDialog(Component parent, String message,
			String fileLocation) throws IOException{
		switch(JOptionPane.showConfirmDialog(parent, message)){
			case JOptionPane.YES_OPTION:
				save(fileLocation);
				return true;
			case JOptionPane.NO_OPTION:
				return true;
			case JOptionPane.CANCEL_OPTION:
				return false;
			case JOptionPane.CLOSED_OPTION:
				return false;
			default:
				throw new IllegalArgumentException(
						"Illegal Dialog Box Selection Option; Code: 207-02");
		}
	}
}
