import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class P2PExample extends MIDlet{
    Display display;
    First fo;
    static DataStore ds=new DataStore("datagram://127.0.0.1");
    public P2PExample(){
        display=Display.getDisplay(this);
        fo=new First(this);
    }
    public void startApp(){
        display.setCurrent(fo);
    }
    public void pauseApp(){
    }
    public void destroyApp(boolean unconditional) {
        display.setCurrent(null);
    }
}
class First extends Form implements CommandListener{
    P2PExample p;
    DataStore ds=P2PExample.ds;
    ChoiceGroup cg;
    Command co=new Command("Load",Command.OK,2);
    Command com=new Command("Search",Command.BACK,2);
    public First(P2PExample p){
        super("P2PExample");
        this.p=p;
        addCommand(co);
        addCommand(com);
        setCommandListener(this);
        cg=new ChoiceGroup("Files shared: ",ChoiceGroup.EXCLUSIVE);
        doThings();
    }
    public void commandAction(Command c,Displayable d){
        if(c==co){
            int v=cg.getSelectedIndex();
            if(v==0){
                Sinfz si=new Sinfz(p);
                p.display.setCurrent(si);
            }
            else{
                Sinf si=new Sinf(p,cg.getString(v));
                p.display.setCurrent(si);
            }
        }
        else{
            p.display.setCurrent(new Sercher(p));
        }
    }
    public void doThings(){
        String[]s=ds.getFileNames();
        cg.append("new text",null);
        for(int i=0;i<s.length;i++){
            cg.append(s[i],null);
        }
        append(cg);
    }
}
class Sinf extends TextBox implements CommandListener{
    Command cp=new Command("Back",Command.BACK,2);
    P2PExample p;
    String f;
    String tstr="";
    public Sinf(P2PExample p,String f){
        super("Share info","",1000,0);
        this.p=p;
        this.f=f;
        getData();
        addCommand(cp);
        setCommandListener(this);
    }
    public void getData(){
        DiskInputStream dis=p.fo.ds.getInputStream(f);
        int u=dis.getSize();
        byte[]ub=dis.readBytes(u);
        for(int i=0;i<ub.length;i++){
            tstr=tstr+(char)ub[i];
        }
        setString(tstr);
    }
    public void commandAction(Command c,Displayable d){
        if(c==cp){
            p.fo=new First(p);
            p.display.setCurrent(p.fo);
        }
    }
}
class Sinfz extends TextBox implements CommandListener{
    Command cp=new Command("Save",Command.OK,2);
    P2PExample p;
    String f;
    String tstr="";
    public Sinfz(P2PExample p){
        super("New Text","",1000,0);
        this.p=p;
        addCommand(cp);
        setCommandListener(this);
    }
    public void commandAction(Command c,Displayable d){
        if(c==cp)p.display.setCurrent(new Saver(p,getString()));
    }
}
class Saver extends Form implements CommandListener{
    Command cp=new Command("Save",Command.OK,2);
    P2PExample p;
    String f;
    String str="";
    TextField t;
    public Saver(P2PExample p,String str){
        super("Saver");
        this.p=p;
        this.str=str;
        t=new TextField("filename: ","",20,TextField.ANY);
        append(t);
        addCommand(cp);
        setCommandListener(this);
    }
    public void commandAction(Command c,Displayable d){
        int v=str.length();
        byte[]b=new byte[v];
        for(int i=0;i<v;i++){
            b[i]=(byte)str.charAt(i);
        }
        String fna=t.getString();
        DiskOutputStream dos=p.fo.ds.getOutputStream(fna);
        dos.writeBytes(b);
        if(c==cp){
            p.fo=new First(p);
            p.display.setCurrent(p.fo);
        }
    }
}
class Sercher extends Form implements CommandListener{
    Command cp=new Command("Search",Command.OK,2);
    P2PExample p;
    String f;
    String str="";
    TextField t;
    public Sercher(P2PExample p){
        super("Searcher");
        this.p=p;
        this.str=str;
        t=new TextField("Search: ","",20,TextField.ANY);
        append(t);
        addCommand(cp);
        setCommandListener(this);
    }
    public void commandAction(Command c,Displayable d){
        Result[]r=p.fo.ds.search(t.getString());
        if(r.length==0)append("No files found");
        else p.display.setCurrent(new Loader(p,r));
    }
}
class Loader extends Form implements CommandListener{
    P2PExample p;
    DataStore ds;
    ChoiceGroup cg;
    Command co=new Command("Load",Command.OK,2);
    Command com=new Command("Back",Command.BACK,2);
    Result[]r;
    public Loader(P2PExample p,Result[]r){
        super("Load");
        this.r=r;
        this.p=p;
        addCommand(co);
        addCommand(com);
        setCommandListener(this);
        cg=new ChoiceGroup("Files found: ",ChoiceGroup.EXCLUSIVE);
        doThings();
    }
    public void commandAction(Command c,Displayable d){
        if(c==co){
            boolean b=p.fo.ds.download(r[cg.getSelectedIndex()]);
            p.fo=new First(p);
            p.display.setCurrent(p.fo);
        }
        else{
            p.fo=new First(p);
            p.display.setCurrent(p.fo);
        }
    }
    public void doThings(){
        for(int i=0;i<r.length;i++){
            cg.append(r[i].filename,null);
        }
        append(cg);
    }
}

