package smscomposerexample;

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 * Trieda pre posielanie SMS spravy
 * @author Richard Scepko
 */
public class SMSSend {
    /**
     * Text SMS
     */
    private String message;
    /**
     * Cislo, na ktore sa ma sms odoslat
     */
    private String number;
    /**
     * SMS spojenie
     */
    MessageConnection conn;
    /**
     * SMSComposerExample pre pristup k logovaciemu textu
     */
    SMSComposerExample sms;

    /**
     * Konstruktor triedy
     * @param message Text spravy
     * @param number Cislo, na ktore sa ma poslat
     */
    public SMSSend(String message, String number){
        this.message = message;
        this.number = number;
    }

    public SMSSend(){
    }

    /**
     * Poslanie SMS
     */
    public void sendSMS(){
        try {
            //Protokol SMS bez portu s cislom
            String addr = "sms://" + this.number;

            //vytvorenie spojenia na adrese protokolu
            this.conn = (MessageConnection) Connector.open(addr);

            //vytvorenie SMS spravy (text)
            TextMessage msg = (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);

            //Nastavenie textu
            msg.setPayloadText(this.message);

            //A poslanie sms
            this.conn.send(msg);

        } catch (IOException ex) {
            ex.printStackTrace();
            //chyby sa vypisuju do logu
            sms.getTextBox().setString(ex.getMessage());
        }

    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public SMSComposerExample getSms() {
        return sms;
    }

    public void setSms(SMSComposerExample sms) {
        this.sms = sms;
    }

    

}
