import java.awt.*;
import java.applet.*;
import window;

public class crea extends Applet
{
    int Size_x; //Width and height of the Applet
    int Size_y;
    int w;
    int h;
    int space = 15;
    int x_pos = 0;
    int y_pos = 0;

    int cols;
    int rows;
    int account=0;
    int count=0;
    int spalte=0;
    int zeile=0;
    field locus[][];
    fieldnum FieldNumber[];
    parameter Norn[];
    String WinTitle = "Information about ";
    Image Male;
    Image Female;
    Image NoPic;
    Image Back;
    MediaTracker Tracker;

    public void paint(Graphics g)
    {
        String Collums = getParameter("cols");
        cols = Integer.parseInt(Collums);
        String Rows = getParameter("rows");
        rows = Integer.parseInt(Rows);
        field locus[][] = new field[cols][rows];
        Size_x = Integer.parseInt(getParameter("Width"))-1;
        Size_y = Integer.parseInt(getParameter("Height"))-1;
        w = (Size_x / cols);
        h = ((Size_y - ((rows-1) * space))/ rows);
        spalte = 0;
        zeile = 0;
        account=0;
        count=0;
        x_pos=0;
        y_pos=0;

        g.drawRect(0,0,Size_x,Size_y);


        for (int i=0; i< cols; i++)
        {
            for (int ii=0; ii<rows; ii++)
            {
                locus[i][ii]= new field(x_pos, y_pos);
                y_pos = y_pos + h + space;
            }
            y_pos = 0;
            x_pos = x_pos + w;
        }

        fieldnum FieldNumber[] = new fieldnum[(cols * rows)];
        for (int n=0; n < (cols * rows); n++)
        {
            FieldNumber[n] = new fieldnum(locus[spalte][zeile].getCol(), locus[spalte][zeile].getRow(), n, false);
            if (spalte < cols)
            {
                spalte++;
            }
            if (spalte >= cols)
            {
                zeile++;
                spalte=0;
            }

        }

        for(int j=0; j<(cols * rows); j++)
        {
            String test = getParameter(FieldNumber[j].getName());
            if (test == null) test = "deactivated";
            if (test.equals("activated"))
            {
                FieldNumber[j].setUsed(true);
                count++;
            }
            else
            {
                FieldNumber[j].setUsed(false);
            }
        }

        parameter Norn[] = new parameter[count];
            for (int cc=0; cc<(cols * rows); cc++)
            {
                if (FieldNumber[cc].getUsed())
                {
                    String Title = getParameter("Title");
                    String Author = getParameter("Author");
                    String Update = getParameter("Update");
                    String Boxtitle = ("Information about " + Title);

                    String StatusPic;
                    String Param = "Field" + cc;
                    String NornName = getParameter(Param + "Name");
                    String MorePic1 = getParameter(Param + "MorePic1");
                        Image aPic2 = getImage(getDocumentBase(), MorePic1);
                        String MoreCom1 = getParameter(Param + "MorePic1Comment");
                    String MorePic2 = getParameter(Param + "MorePic2");
                        Image aPic3 = getImage(getDocumentBase(), MorePic2);
                        String MoreCom2 = getParameter(Param + "MorePic2Comment");
                    String NornStatus = getParameter(Param + "Status");
                        if (NornStatus == null) NornStatus = "n/a";
                        else if (NornStatus.equals("alive") || NornStatus.equals("unborn") || NornStatus.equals("deceased"));
                        else NornStatus = "Invalid Input Parameter!";
                        if (NornStatus.equals("deceased")) StatusPic = "rip.gif";
                        else if (NornStatus.equals("unborn")) StatusPic = "egg.gif";
                        else StatusPic = "";
                    Image StatPic = getImage(getDocumentBase(), StatusPic);
                    String NornSex = getParameter(Param + "Sex");
                    Image SexPic = getImage(getDocumentBase(), "quest.gif");
                        if (NornSex==null) NornSex="n/a";
                        else if (NornSex.equals("male")) SexPic = getImage(getDocumentBase(), "male.gif");
                        else if (NornSex.equals("female")) SexPic = getImage(getDocumentBase(), "female.gif");
                        else NornSex = "Invalid Sex Parameter";
                    String Developed = getParameter(Param + "Developed");
                        if (Developed == null) Developed = "n/a";
                        else if (Developed.equals("yes") || Developed.equals("no"));
                        else Developed = "Invalid Parameter";
                    String Preg = getParameter(Param + "Pregnant");
                    String Pregnant;
                        if (Preg == null) Pregnant = "no";
                        else if (Preg.equals("yes") || Preg.equals("no")) Pregnant = Preg;
                        else Pregnant = "invalid Parameter";
                        if (Developed.equals("no")) Pregnant = "impossible";
                        if (NornSex.equals("male")) Pregnant = "impossible";
                    String NornAge = getParameter(Param + "Age");
                    String NornMom = getParameter(Param + "Mom");
                    String NornDad = getParameter(Param + "Dad");
                        if (NornMom == null) NornMom = "unknown";
                        if (NornDad == null) NornDad = "unknown";
                    int Position = cc;
                    String Picture = getParameter(Param + "Picture");
                        if (Picture == null) Picture = "nopic.gif";
                    String PicComment = getParameter(Param + "PicCom");
                    Image aPic = getImage(getDocumentBase(), Picture);
                    String Comment = getParameter(Param + "Comment");
                        if (Comment == null) Comment = "No further comment.";
                    Norn[account] = new parameter(Title, Author, Update, Boxtitle, Position, NornName, NornSex, NornAge, NornMom, NornDad, aPic, Picture, PicComment, NornStatus, Comment, StatPic, MorePic1, aPic2, MoreCom1, MorePic2, aPic3, MoreCom2, SexPic, Pregnant, Developed);

                    account++;
                }
            }

        Male = getImage(getDocumentBase(), "male.gif");
        Female = getImage(getDocumentBase(), "female.gif");
        NoPic = getImage(getDocumentBase(), "nopic.gif");
        Back = getImage(getDocumentBase(), "clouds.jpg");
        Image persPic[];

        int persPicCounter = 0;
        for (int im = 0; im < account; im++)
        {
            if (Norn[im].getPicName() == null);
            else
            {
                persPicCounter++;
            }
        }

        persPic = new Image[persPicCounter];
        int ppcc=0;

        for (int ppc = 0; ppc < account; ppc++)
        {
            if (Norn[ppc].getPicName() == null);
            else
            {
                persPic[ppcc] = Norn[ppc].getPic();
                ppcc++;
            }
        }



        int statPicCounter = 0;
        for (int im2 = 0; im2 < account; im2++)
        {
            String pruef = Norn[im2].getStatus();
            if (pruef.equals("n/a") || pruef.equals("alive"));
            else
            {
                statPicCounter++;
            }
        }

        Image StatPic[] = new Image[statPicCounter];
        int ppcc2=0;

        for (int ppc2 = 0; ppc2 < account; ppc2++)
        {
            if (Norn[ppc2].getStatus().equals("n/a") || Norn[ppc2].getStatus().equals("alive"));
            else
            {
                StatPic[ppcc2] = Norn[ppc2].getStatPic();
                ppcc2++;
            }
        }

        Tracker = new MediaTracker(this);
        Tracker.addImage(Male, 0);
        Tracker.addImage(Female, 1);
        Tracker.addImage(NoPic, 2);
        Tracker.addImage(Back, 3);

        MediaTracker Tracker2 = new MediaTracker(this);
        for (int v=0; v<persPicCounter; v++)
        {
            Tracker2.addImage(persPic[v], v);
        }

        MediaTracker Tracker3 = new MediaTracker(this);
        for (int vv = 0; vv < statPicCounter; vv++)
        {
            Tracker3.addImage(StatPic[vv], vv);
        }

        showStatus("Waiting for the Creatures-Applet-Images to be loaded.");
        try
        {
            Tracker.waitForAll();
            Tracker2.waitForAll();
            Tracker3.waitForAll();
        }
        catch(InterruptedException e)
        {
            return;
        }

        showStatus("The Creatures-Applet has been initialized correctly!");
        int ju = 0;
        int ju2 = 0;

        //Drawing of the Background-Picture.
        int xTimes = (Size_x / 216) + 1; //216 = Width of the Background-Image.
        int yTimes = (Size_y / 144) + 1; //144 = Height "  "      "        "  .
        int xp = 0; //X-Position.
        int yp = 0; //Y-Position.
        for (int b=0; b < xTimes; b++)
        {
            for (int bb=0; bb < yTimes; bb++)
            {
                g.drawImage(Back, xp, yp, this);
                yp = yp + 144; //144 = Height
            }
            yp = 0;
            xp = xp + 216; //216 = Width
        }



            for (int hh=0; hh < account; hh++)
            {
                    int location = Norn[hh].getPos();

                    //Drawing of the Status Pictures (deceased or unborn).
                    if(Norn[hh].getStatus().equals("deceased") || Norn[hh].getStatus().equals("unborn"))
                    {
                        g.drawImage(StatPic[ju2], FieldNumber[location].getCol() + (w/2) + 11, FieldNumber[location].getRow(), this);
                        ju2++;
                    }

                    if(Norn[hh].getPicName() == null)
                    {

                        g.drawImage(NoPic, FieldNumber[location].getCol(), (FieldNumber[location].getRow()+ 24),w,(h-40), this);
                    }
                    else    //Drawing of personal pictures if mentioned in the HTML-file.
                    {
                        g.drawImage(persPic[ju], FieldNumber[location].getCol(), (FieldNumber[location].getRow() + 24), w, (h-40), this);
                        ju++;
                    }

                    //Drawing of the Norn's name.
                    g.drawString(Norn[hh].getName(), (FieldNumber[location].getCol() + (w/10)), FieldNumber[location].getRow()+h-5);

                    //Drawing of the male/female images.
                    if (Norn[hh].getSex().equals("male"))
                    {
                        g.drawImage(Male, FieldNumber[location].getCol() + (w/2) - 11, FieldNumber[location].getRow(), this);
                    }
                    else if(Norn[hh].getSex().equals("female"))
                    {
                        g.drawImage(Female, FieldNumber[location].getCol() + (w/2) - 11, FieldNumber[location].getRow(), this);
                    }

                    //Drawing of the rectangles.
                    g.drawRect(FieldNumber[location].getCol(), FieldNumber[location].getRow(), w, h);
            }


    int x_pos = 0;
    int y_pos = 0;
        for (int i = 1; i <= cols; i++)
        {
         for (int ii=1; ii<= rows; ii++)
         {
            y_pos = y_pos + h + space;
         }
         y_pos=0;
         x_pos = x_pos + w;

        }

    for (int f=0; f<count; f++)
    {
        if (Norn[f].getMom().equals("unknown") || Norn[f].getDad().equals("unknown"));
        else
        {
            field Vert[] = new field[1];
            int x_kor_fe;
            int y_kor_fe;
            String Mama = Norn[f].getMom();
            for (int mm=0; mm<count; mm++)
            {
                if (Mama.equals(Norn[mm].getName()))
                {
                    int MamsPos = Norn[mm].getPos();
                    //Position to paint the vertical line in the middle of the field.
                    x_kor_fe = FieldNumber[MamsPos].getCol() + Math.abs(w / 2);
                    y_kor_fe = FieldNumber[MamsPos].getRow() + h;
                    Vert[0] = new field(x_kor_fe, y_kor_fe);
                    g.drawLine(x_kor_fe, y_kor_fe, x_kor_fe, y_kor_fe + Math.abs(space/2));
                }
            }
            String Papa = Norn[f].getDad();
            for (int ff=0; ff<count; ff++)
            {
                if (Papa.equals(Norn[ff].getName()))
                {
                    int PapsPos = Norn[ff].getPos(); //Position to draw the vertical line in the middle of the field.
                    int x_kor = FieldNumber[PapsPos].getCol() + Math.abs(w / 2);
                    int y_kor = FieldNumber[PapsPos].getRow() + h;
                    g.drawLine(x_kor, y_kor, x_kor, y_kor + Math.abs(space/2));
                    g.drawLine(x_kor, y_kor + Math.abs(space/2), Vert[0].getCol(), Vert[0].getRow() + Math.abs(space/2));  //Draws the lines between the mother and the father.
                }
            }

            int Child = Norn[f].getPos();
            int x_kor_child = FieldNumber[Child].getCol() + Math.abs(w/2);
            int y_kor_child = FieldNumber[Child].getRow();
            g.drawLine(x_kor_child, y_kor_child, x_kor_child, y_kor_child - Math.abs(space/2));
        }
    }

    }


    //***************************************************************************************

    public boolean mouseDown(Event e, int r_pos, int d_pos)
    {
        if (window.x == 1)
        {
        String Collums = getParameter("cols");
        cols = Integer.parseInt(Collums);
        String Rows = getParameter("rows");
        rows = Integer.parseInt(Rows);
        field locus[][] = new field[cols][rows];
        Size_x = Integer.parseInt(getParameter("Width"))-1;
        Size_y = Integer.parseInt(getParameter("Height"))-1;
        w = Size_x / cols;
        h = Size_y / rows;
        zeile = 0;
        spalte = 0;
        account=0;
        count=0;
        x_pos=0;
        y_pos=0;
        for (int i=0; i< cols; i++)
            {
                for (int ii=0; ii<rows; ii++)
                {
                    locus[i][ii]= new field(x_pos, y_pos);
                    y_pos = y_pos + h + space;
                }
                y_pos = 0;
                x_pos = x_pos + w;
            }
        fieldnum FieldNumber[] = new fieldnum[(cols * rows)];
        for (int n=0; n < (cols * rows); n++)
        {
            FieldNumber[n] = new fieldnum(locus[spalte][zeile].getCol(), locus[spalte][zeile].getRow(), n, false);
            if (spalte < cols)
            {
                spalte++;
            }
            if (spalte >= cols)
            {
                zeile++;
                spalte=0;
            }
        }

        for(int j=0; j<(cols * rows); j++)
        {
            String test = getParameter(FieldNumber[j].getName());
            if (test == null) test = "deactivated";
            if (test.equals("activated"))
            {
                FieldNumber[j].setUsed(true);
                count++;
            }
            else
            {
                FieldNumber[j].setUsed(false);
            }
        }

        parameter Norn[] = new parameter[count];
            for (int cc=0; cc<(cols * rows); cc++)
            {
                if (FieldNumber[cc].getUsed())
                {
                    String Title = getParameter("Title");
                    String Author = getParameter("Author");
                    String Update = getParameter("Update");
                    String Boxtitle = ("Information about " + Title);

                    String StatusPic;
                    String Param = "Field" + cc;
                    String NornName = getParameter(Param + "Name");
                    String MorePic1 = getParameter(Param + "MorePic1");
                        Image aPic2 = getImage(getDocumentBase(), MorePic1);
                        String MoreCom1 = getParameter(Param + "MorePic1Comment");
                    String MorePic2 = getParameter(Param + "MorePic2");
                        Image aPic3 = getImage(getDocumentBase(), MorePic2);
                        String MoreCom2 = getParameter(Param + "MorePic2Comment");
                    String NornStatus = getParameter(Param + "Status");
                        if (NornStatus == null) NornStatus = "n/a";
                        else if (NornStatus.equals("alive") || NornStatus.equals("unborn") || NornStatus.equals("deceased"));
                        else NornStatus = "Invalid Input Parameter!";
                        if (NornStatus.equals("deceased")) StatusPic = "rip.gif";
                        else if (NornStatus.equals("unborn")) StatusPic = "egg.gif";
                        else StatusPic = "";
                    Image StatPic = getImage(getDocumentBase(), StatusPic);
                    String NornSex = getParameter(Param + "Sex");
                    Image SexPic = getImage(getDocumentBase(), "quest.gif");
                        if (NornSex==null) NornSex="n/a";
                        else if (NornSex.equals("male")) SexPic = getImage(getDocumentBase(), "male.gif");
                        else if (NornSex.equals("female")) SexPic = getImage(getDocumentBase(), "female.gif");
                        else NornSex = "Invalid Sex Parameter";
                    String Developed = getParameter(Param + "Developed");
                        if (Developed == null) Developed = "n/a";
                        else if (Developed.equals("yes") || Developed.equals("no"));
                        else Developed = "Invalid Parameter";
                    String Preg = getParameter(Param + "Pregnant");
                    String Pregnant;
                        if (Preg == null) Pregnant = "no";
                        else if (Preg.equals("yes") || Preg.equals("no")) Pregnant = Preg;
                        else Pregnant = "invalid Parameter";
                        if (Developed.equals("no")) Pregnant = "impossible";
                        if (NornSex.equals("male")) Pregnant = "impossible";
                    String NornAge = getParameter(Param + "Age");
                    String NornMom = getParameter(Param + "Mom");
                    String NornDad = getParameter(Param + "Dad");
                        if (NornMom == null) NornMom = "unknown";
                        if (NornDad == null) NornDad = "unknown";
                    int Position = cc;
                    String Picture = getParameter(Param + "Picture");
                        if (Picture == null) Picture = "nopic.gif";
                    String PicComment = getParameter(Param + "PicCom");
                    Image aPic = getImage(getDocumentBase(), Picture);
                    String Comment = getParameter(Param + "Comment");
                        if (Comment == null) Comment = "No further comment.";
                    Norn[account] = new parameter(Title, Author, Update, Boxtitle, Position, NornName, NornSex, NornAge, NornMom, NornDad, aPic, Picture, PicComment, NornStatus, Comment, StatPic, MorePic1, aPic2, MoreCom1, MorePic2, aPic3, MoreCom2, SexPic, Pregnant, Developed);

                    account++;
                }
            }

            for (int v=0; v<account; v++)
            {
                int akut = Norn[v].getPos();
                int hor_kor = FieldNumber[akut].getCol();
                int ver_kor = FieldNumber[akut].getRow();

                if (r_pos > hor_kor && r_pos < hor_kor + w && d_pos > ver_kor && d_pos < ver_kor + h)
                {
                    String WindowTitle = WinTitle + Norn[v].getName();
                    window win = new window(Norn[v].getTitle(), Norn[v].getAuthor(), Norn[v].getUpdate(), Norn[v].getBoxtitle(), this, Norn[v].getName(), Norn[v].getSex(), Norn[v].getAge(), Norn[v].getMom(), Norn[v].getDad(), Norn[v].getPic(), Norn[v].getPicName(), Norn[v].getPicCom(), Norn[v].getStatus(), Norn[v].getComment(), Norn[v].getStatPic(), Norn[v].getNewPicName(), Norn[v].getNewPic(), Norn[v].getMoreCom1(), Norn[v].getNewPicName2(), Norn[v].getNewPic2(), Norn[v].getMoreCom2(), Norn[v].getSexPic(), Norn[v].getPreg(), Norn[v].getDev());
                    win.setBackground(Color.white);
                    win.setTitle(WindowTitle);
                    win.resize(400,400);
                    win.show();
                    WindowTitle= WinTitle;
                }
            }
    }//End of x == 1.
    return true;
    }


} // End of public class crea


class field
{
    int let;
    int num;

    public field(int Numb, int Letter)
    {
        let = Letter;
        num = Numb;
    }

    public int getRow()
    {
        return let;
    }

    public int getCol()
    {
        return num;
    }
}


class fieldnum extends field
{
    String Name="Field";
    String Stat;
    boolean Status;
    int Zahl;
    boolean used;

    public fieldnum(int Numb, int Letter, int Call, boolean use)
    {
         super(Numb, Letter);
         Zahl = Call;
         used = use;
    }

    public String getName()
    {
        Name = Name + Zahl;
        return Name;
    }

    public boolean setUsed(boolean Status)
    {
        used = Status;
        return used;
    }

    public boolean getUsed()
    {
        return used;
    }
}

class parameter
{
    String Alter;
    String Norn;
    String Geschlecht;
    String Mutter;
    String Vater;
    int Pos;
    Image Bild;
    String BildName;
    String BildKom;
    String Status;
    String Kommentar;
    Image StatusBild, SexPic;
    String neuer_PicName, neuer_PicName2;
    Image neues_bild, neues_bild2;
    String MoreCom1, MoreCom2;
    String Title2, Author2, Update2, Boxtitle2;
    String Schwanger, Fruchtbar;

    public parameter(String Title, String Author, String Update, String Boxtitle, int Position, String Name, String Sex, String Age, String Mother, String Father, Image Picture, String Name_of_Picture, String Picture_Comment, String actual_Status, String Comment_Norn, Image Status_Picture, String AnotherPictureName, Image AnotherPic, String CommentOfAnotherPic1, String AnotherPictureName2, Image AnotherPic2, String CommentOfAnotherPic2, Image SexPicture, String Pregnant, String Developed)
    {
        Schwanger = Pregnant;
        Fruchtbar = Developed;
        SexPic = SexPicture;
        Pos = Position;
        Norn = Name;
        Geschlecht = Sex;
        Alter = Age;
        Mutter = Mother;
        Vater = Father;
        Bild = Picture;
        BildName = Name_of_Picture;
        BildKom = Picture_Comment;
        Status = actual_Status;
        Kommentar = Comment_Norn;
        StatusBild = Status_Picture;
        neuer_PicName = AnotherPictureName;
        neues_bild = AnotherPic;
        MoreCom1 = CommentOfAnotherPic1;
        neuer_PicName2 = AnotherPictureName2;
        neues_bild2 = AnotherPic2;
        MoreCom2 = CommentOfAnotherPic2;
        Title2 = Title;
        Author2 = Author;
        Update2 = Update;
        Boxtitle2 = Boxtitle;
    }

    public String getPreg()
    {
        return Schwanger;
    }

    public String getDev()
    {
        return Fruchtbar;
    }

    public Image getSexPic()
    {
        return SexPic;
    }

    public String getTitle()
    {
        return Title2;
    }

    public String getAuthor()
    {
        return Author2;
    }

    public String getUpdate()
    {
        return Update2;
    }

    public String getBoxtitle()
    {
        return Boxtitle2;
    }

    public String getMoreCom1()
    {
        return MoreCom1;
    }

    public String getNewPicName()
    {
        return neuer_PicName;
    }

    public Image getNewPic()
    {
        return neues_bild;
    }

    public String getMoreCom2()
    {
        return MoreCom2;
    }

    public String getNewPicName2()
    {
        return neuer_PicName2;
    }

    public Image getNewPic2()
    {
        return neues_bild2;
    }


    public Image getStatPic()
    {
        return StatusBild;
    }

    public String getComment()
    {
        return Kommentar;
    }

    public String getStatus()
    {
        return Status;
    }

    public String getPicCom()
    {
        return BildKom;
    }

    public String getPicName()
    {
        return BildName;
    }

    public int getPos()
    {
        return Pos;
    }

    public String getName()
    {
        return Norn;
    }

    public String getSex()
    {
        return Geschlecht;
    }

    public String getAge()
    {
        return Alter;
    }

    public String getMom()
    {
        return Mutter;
    }

    public String getDad()
    {
        return Vater;
    }

    public Image getPic()
    {
        return Bild;
    }

}