package sms;

import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.obex.*;

/**
 * Trieda Client pre pripajanie sa na Bluetooth server (telefon)
 * @author Richard Scepko
 */
public class Client {

    /**
     * Objekt triedy ServicesSearch pre najdenie sluzieb na zariadeni
     */
    private ServicesSearch servicesSearch;
    /**
     * SmsView pre pristup ku komponentom okna smsView
     */
    private SmsView sms;

    /**
     * Konstruktor triedy Client
     * @param servicesSearch Objekt triedy ServicesSearch pre najdenie sluzieb na zariadeni
     * @param sms SmsView pre pristup ku komponentom okna smsView
     */
    public Client(ServicesSearch servicesSearch, SmsView sms){
        this.servicesSearch = servicesSearch;
        this.sms = sms;
    }

    /**
     * Spustenie klienta
     * @param args Adresa servera
     * @throws IOException
     * @throws InterruptedException
     */
    public void run(String[] args)throws IOException, InterruptedException {
        String serverURL = null; // = "btgoep://0019639C4007:6";
        if ((args != null) && (args.length > 0)) {
            serverURL = args[0]; //adresa servera
        }
        if (serverURL == null) {
            String[] searchArgs = null;
            if (servicesSearch.serviceFound.size() == 0) {
                System.out.println("OBEX service not found");
                return;
            }
            // Select the first service found
            serverURL = (String)servicesSearch.serviceFound.elementAt(0);
        }

        System.out.println("Connecting to " + serverURL);

        //Pripajanie na server
        ClientSession clientSession = (ClientSession) Connector.open(serverURL);
        HeaderSet hsConnectReply = clientSession.connect(null);
        if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
            System.out.println("Failed to connect");
            return;
        }

        //Nastavenie hlavicky posielanych dat
        HeaderSet hsOperation = clientSession.createHeaderSet();  
        hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
        hsOperation.setHeader(HeaderSet.TYPE, "text");

        //Create PUT Operation
        Operation putOperation = clientSession.put(hsOperation);

        //Vytvorenie posielajucej spravy : [cislo]~[cislo]~[cislo]~[cislo]~[cislo]~sprava
        String send = "";
        if(!((User)sms.getjComboBox1().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox1().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox2().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox2().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox3().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox3().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox4().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox4().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox5().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox5().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox7().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox7().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox8().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox8().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox9().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox9().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox10().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox10().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox11().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox11().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox12().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox12().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox13().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox13().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox14().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox14().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox15().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox15().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox16().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox16().getSelectedItem()).getNumber()+"~";
        }
        if(!((User)sms.getjComboBox17().getSelectedItem()).getName().equals("...")){
            send+=((User)sms.getjComboBox17().getSelectedItem()).getNumber()+"~";
        }
        send+=sms.getjTextArea1().getText();
        System.out.println("Sending: " + send);
        byte data[] = send.getBytes("iso-8859-1");
        OutputStream os = putOperation.openOutputStream();
        //posielanie dat
        os.write(data);
        os.close();

        //ukoncenie spojenia
        putOperation.close();

        clientSession.disconnect(null);

        clientSession.close();
    }
}
