package meganetpo.picturehandler;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import javax.swing.Timer;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import meganetpo.Player;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;
/**
*
* @author Burtovoy Semen
*/
public class PictureHandler implements Runnable, ActionListener {
private static final Logger logger = Logger.getLogger(PictureHandler.class);
private BufferedImage originalImage = null;
private File imageFile = null;
private PictureProcesor proc = null;
LetterDataBase base;
private boolean isInit = false;
private int process = 0;
private URL url = null;
private HttpDownloader download;
private Timer timer = null;
private DefaultListModel listModel =null;
private DefaultListModel FindListModel = new DefaultListModel();
private PropertiesConfiguration conf = picture.Main.config;
private Integer autoupdateVal;
private boolean autoupdate;
public PictureHandler() {
autoupdate = conf.getBoolean("picturehandler.doautoupdate");
autoupdateVal = conf.getInt("picturehandler.autoupdate");
}
private void init() throws IOException {
base = new LetterDataBase();
download = new HttpDownloader();
process = 1;
update();
}
public ArrayList<PixelArray> getAlphabet(ArrayList<ArrayList<PixelArray>> letterList) {
ArrayList<PixelArray> alphabet = new ArrayList<PixelArray>();
for (int i = 0; i < letterList.size(); i++) {
for (int j = 0; j < letterList.get(i).size(); j++) {
boolean flag = true;
PixelArray letter = letterList.get(i).get(j);
for (int k = 0; k < alphabet.size(); k++) {
if (alphabet.get(k).equals(letter)) {
flag = false;
}
}
if (flag) {
alphabet.add(letter);
}
}
}
return alphabet;
}
public boolean checkNick(String nick) throws NoSuchElementException {
ArrayList<Integer> hashWord = new ArrayList<Integer>();
for (int i = 0; i < nick.length(); i++) {
String chr = Character.toString(nick.charAt(i));
hashWord.add(base.getLetterHash(chr));
}
int nickHash = hashWord.hashCode();
ArrayList<Integer> hashWordList = proc.getHashWordList();
for (int i = 0; i < hashWordList.size(); i++) {
if (nickHash == hashWordList.get(i)) {
return true;
}
}
return false;
}
public void checkAllNicks() {
if(listModel == null)
return;
FindListModel.clear();
ArrayList<Integer> hashWord;
ArrayList<Integer> hashWordList = new ArrayList<Integer>();
for (int i = 0; i < listModel.size(); i++) {
hashWord = new ArrayList<Integer>();
Player player = (Player) listModel.getElementAt(i);
String nick = player.getName();
for (int j = 0; j < nick.length(); j++) {
String chr = Character.toString(nick.charAt(j));
hashWord.add(base.getLetterHash(chr));
}
int nickHash = hashWord.hashCode();
hashWordList.add(nickHash);
}
ArrayList<Integer> baseHashWordList = proc.getHashWordList();
for (int i = 0; i < hashWordList.size(); i++) {
int nickHash = hashWordList.get(i);
for (int j = 0; j < baseHashWordList.size(); j++) {
if (nickHash == baseHashWordList.get(j)) {
FindListModel.addElement(listModel.get(i));
break;
}
}
}
}
public boolean isIsInit() {
return isInit;
}
public int getProcess() {
return process;
}
public void run() {
timer = new Timer(autoupdateVal, this);
this.imageFile = new File(conf.getString("picturehandler.image"));
try {
init();
} catch (IOException ex) {
logger.error(ex);
}
process++;
isInit = true;
Thread.yield();
}
public void update() throws IOException {
timer.stop();
getPicture();
process = 2;
try {
originalImage = ImageIO.read(imageFile);
proc = new PictureProcesor(originalImage);
process = 3;
proc.separateForWord();
Thread.yield();
process = 4;
proc.separateForLetter();
Thread.yield();
process = 5;
proc.clean();
} catch (IOException ex) {
logger.error(ex);
}
checkAllNicks();
timerRestar();
}
private void getPicture() throws IOException {
try {
url = new URL(conf.getString("picturehandler.url"));
download.urlDownload(url, imageFile);
process++;
} catch (FileNotFoundException ex) {
logger.error(ex);
} catch (MalformedURLException ex) {
logger.error(ex);
} catch (UnknownHostException ex) {
logger.error(ex);
}
}
public void timerRestar() {
if(!autoupdate) {
return;
}
timer.setDelay(autoupdateVal);
if (timer.isRunning()) {
timer.restart();
}
else {
timer.start();
}
}
public void actionPerformed(ActionEvent e) {
try {
update();
} catch (IOException ex) {
logger.error(ex);
}
}
public DefaultListModel getFindList() {
return FindListModel;
}
public void setFindList(DefaultListModel FindList) {
this.FindListModel = FindList;
}
public DefaultListModel getListModel() {
return listModel;
}
public void setListModel(DefaultListModel listModel) {
this.listModel = listModel;
}
public boolean isAutoupdate() {
return autoupdate;
}
public void setAutoupdate(boolean autoupdate) {
this.autoupdate = autoupdate;
}
public Integer getAutoupdateVal() {
return autoupdateVal;
}
public void setAutoupdateVal(Integer autoupdateVal) {
this.autoupdateVal = autoupdateVal;
}
}