// Copyright Andy Deck 2002, GNU Public License 2
// With the notable provision that any commercial use
// must be approved by the copyright holder.
import java.awt.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.FilterInputStream;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.zip.GZIPInputStream;

public class oil extends Module
    implements Runnable
{

    public oil()
    {
        xOff = Module.MIDWID;
        yOff = Module.MIDHT;
    }

    public void init(lexicon lexicon1, String s)
    {
        super.lex = lexicon1;
        try
        {
            StringTokenizer stringtokenizer = new StringTokenizer(s);
            switch(stringtokenizer.countTokens())
            {
            case 1: // '\001'
                xOff = Integer.parseInt(stringtokenizer.nextToken());
                break;

            case 2: // '\002'
                xOff = Integer.parseInt(stringtokenizer.nextToken());
                yOff = Integer.parseInt(stringtokenizer.nextToken());
                break;
            }
            if(xOff < 0 || xOff > Module.WIDTH)
                xOff = Module.MIDWID;
            if(yOff < 0 || yOff > Module.HEIGHT)
                yOff = Module.MIDHT;
        }
        catch(Exception exception)
        {
            xOff = yOff = Module.MIDWID;
        }
        start();
    }

    public void stop()
    {
        t = null;
    }

    public void start()
    {
        t = new Thread(this, "oil");
        t.start();
    }

    public int getMid(String s, int i)
    {
        int j = s.indexOf('-', i + 2) - 1;
        int k = s.indexOf(',', i);
        if(k == -1)
            k = j;
        else
        if(j > -1)
            k = k >= j ? j : k;
        return k;
    }

    public int getEnd(String s, int i)
    {
        int j;
        for(j = i; j < s.length(); j++)
        {
            char c = s.charAt(j);
            if((c < '0' || c > '9') && c != '.' && c != '-')
                break;
        }

        return j;
    }

    public void run()
    {
        try
        {
            URL url = new URL(Module.documentBase + "oil.dat.gz");
            DataInputStream datainputstream = new DataInputStream(new GZIPInputStream(url.openStream()));
            StringBuffer stringbuffer = new StringBuffer();
            String s;
            while((s = datainputstream.readLine()) != null && !super.abort) 
                stringbuffer.append(s);
            StringTokenizer stringtokenizer = new StringTokenizer(stringbuffer.toString(), "#");
            Polygon apolygon[] = new Polygon[stringtokenizer.countTokens()];
            int i = 0;
            double d = 0.0D;
            double d1 = 0.0D;
            while(stringtokenizer.hasMoreTokens() && !super.abort) 
            {
                String s1 = stringtokenizer.nextToken();
                for(int j = 0; j < s1.length(); apolygon[i].addPoint((int)((d / 512D) * (double)Module.WIDTH), (int)((d1 / 512D) * (double)Module.HEIGHT)))
                    switch(s1.charAt(j++))
                    {
                    case 108: // 'l'
                        int i2 = getMid(s1, j);
                        int k = getEnd(s1, i2 + 1);
                        d += Double.valueOf(s1.substring(j, i2)).doubleValue();
                        d1 += Double.valueOf(s1.substring(i2 + 1, k)).doubleValue();
                        break;

                    case 77: // 'M'
                        apolygon[i] = new Polygon();
                        // fall through

                    case 76: // 'L'
                        int j2 = getMid(s1, j);
                        int l = getEnd(s1, j2 + 1);
                        d = Double.valueOf(s1.substring(j, j2)).doubleValue();
                        d1 = Double.valueOf(s1.substring(j2 + 1, l)).doubleValue();
                        break;

                    case 104: // 'h'
                        int i1 = getEnd(s1, j);
                        d += Double.valueOf(s1.substring(j, i1)).doubleValue();
                        break;

                    case 72: // 'H'
                        int j1 = getEnd(s1, j);
                        d = Double.valueOf(s1.substring(j, j1)).doubleValue();
                        break;

                    case 118: // 'v'
                        int k1 = getEnd(s1, j);
                        d1 += Double.valueOf(s1.substring(j, k1)).doubleValue();
                        break;

                    case 86: // 'V'
                        int l1 = getEnd(s1, j);
                        d1 = Double.valueOf(s1.substring(j, l1)).doubleValue();
                        break;
                    }

                i++;
            }
            datainputstream.close();
            Graphics g = super.lex.img.getGraphics();
            setRenderMode(g);
            for(float f = 0.0F; !super.abort && (f = (float)((double)f - 0.20000000000000001D)) > -6F;)
            {
                for(int k2 = 0; k2 < apolygon.length; k2++)
                {
                    Polygon polygon = new Polygon();
                    for(int l2 = 0; l2 < apolygon[k2].npoints; l2++)
                        polygon.addPoint(xOff + (int)(Math.cos(f) * (double)apolygon[k2].xpoints[l2] - Math.sin(f) * (double)apolygon[k2].ypoints[l2]), yOff + (int)(Math.sin(f) * (double)apolygon[k2].xpoints[l2] + Math.cos(f) * (double)apolygon[k2].ypoints[l2]));

                    g.setColor(k2 != 0 && k2 != 2 && k2 != 4 ? Color.white : Color.black);
                    g.fillPolygon(polygon);
                }

                super.repaint();
                oil _tmp = this;
                Thread.sleep(200L);
            }

            g.dispose();
        }
        catch(Exception exception) { }
    }

    private volatile Thread t;
    int xOff;
    int yOff;
}
