package com.jasamedika.medifirst2000.service.impl;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.dao.SettingDataFixedDao;
import com.jasamedika.medifirst2000.service.SocketClientService;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Service("SocketClientService")
public class SocketClientServiceImpl implements SocketClientService{

	private Socket requestSocket;
	
	private Map<String, Object> result = new HashMap<>();
	
	@Override
	public Map<String, Object> connectToWinSock(final Map<String, Object> vo){
		try {
			requestSocket = new Socket(InetAddress.getByName((String) vo.get("ipAddress")),(Integer) vo.get("port"));
			if(requestSocket.isConnected()){
				new Timer().schedule(new TimerTask(){
					@Override
			        public void run() {
						ObjectOutputStream sendMessage;
						try {
							sendMessage = new ObjectOutputStream(requestSocket.getOutputStream());
							sendMessage.writeObject(vo.get("noRec"));
							result = vo;
			                cancel();
						} catch (IOException e) {
							result = null;
						}
					}
			    },1000L, 1000L);	
			}
			requestSocket.close();
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
}
