package yi.iu.processor.clc;

import yi.iu.processor.clc.*;
import yi.iu.processor.ort.*;
import yi.util.ImageUtil;
import yi.util.io.OutputResult;
import yi.iu.processor.clc.*;
import Acme.JPM.Decoders.*;

import java.awt.*;
import java.io.*;
import java.util.*;

public final class ArgumentExperiment
{
    public static int maxSize=0;
    public static int maxSizeAfterSpatial=0;
    public static int maxSizeAfterOverlap=0;
    public static double maxInterSize=0;
    public static double maxInterCombineSize=0;
    public static double maxInterCombineSizeR=0;
    public static double maxSizeW=0;
    public static double maxSizeAfterSpatialW=0;
    public static double maxSizeAfterOverlapW=0;
    public static double maxInterSizeW=0;
    public static double maxInterCombineSizeW=0;
    public static double maxInterCombineSizeWR=0;

    public static int[] rstImg=null;
    static Vector clusters= new Vector();
    static String imageName=null;
    static Dimension d= new Dimension();
    static int[] source;
    static String directory="/images/test";
    static String outFile = null;

    static boolean OUTPUTCLUSTERSIMG= false;
    static boolean OUTPUTRSTIMG= false;
    static int[][] thresholds= {{0,0}};

    public static void interClusters(LineCluster lc0, LineCluster lc1,
        boolean[] inter1, boolean[] inter2,
        int interCount1, int interCount2, int intersectNum)
    {
        if (interCount1<4&&interCount2<4) return;
        if (interCount1+interCount2<5) return;
        int[] sizes= {lc0.size(), lc1.size(), interCount1, interCount2, intersectNum};
        clusters.addElement(sizes);
        //System.err.println(" "+lc0.size()+"->"+lc1.size());


        if (OUTPUTCLUSTERSIMG) {
            boolean satisfy= false;
            for (int i=0;i<thresholds.length;i++) {
                if ( Math.max ( interCount1 , interCount2) >= thresholds[i][0]
                    && Math.max((double)interCount1/lc0.size() ,
                                (double)interCount2/lc1.size())*100>= thresholds[i][1] )
                {
                    satisfy=true;
                    break;
                }
            }
            if (!satisfy) return;

            int [] edgeimg= new int[source.length];
            for (int j=0;j<edgeimg.length;j++) edgeimg[j]=0xffffffff;
            for (int j=0;j<lc0.size();j++) {
                ORTLine l= (ORTLine)lc0.getLineAt(j);
                if (inter1[j])
                    DrawUtil.ort2image(edgeimg, d, l, 0xffff0000, 2);
                else
                    DrawUtil.ort2image(edgeimg, d, l, 0xff7f0000, 1);
            }
            for (int j=0;j<lc1.size();j++) {
                ORTLine l= (ORTLine)lc1.getLineAt(j);
                if (inter2[j])
                    DrawUtil.ort2image(edgeimg, d, l, 0xff0000ff, 2);
                else
                    DrawUtil.ort2image(edgeimg, d, l, 0xff00007f, 1);
            }
            String newName;
            int index=imageName.lastIndexOf(".jpg");
            if (index<0) index=imageName.lastIndexOf(".JPG");
            if (index<0) index=imageName.lastIndexOf(".ppm");
            if (index<0) index=imageName.lastIndexOf(".PPM");
            if (index>=0) newName= imageName.substring(0,index)+"_inter"+clusters.size()+".gif";
            else newName= imageName+"_inter"+clusters.size()+".gif";
            System.err.println("output: "+newName);
            yi.util.io.OutputResult.outputGIFImage(newName, edgeimg, d.width, d.height);

            edgeimg=null;

            System.gc();
        }

    }

    public static void outputClusters(int index)
    {
        try {
            FileWriter out= new FileWriter(outFile, true);
            out.write(imageName+"\t\n");
            for (int i=0;i<clusters.size();i++) {
                int[] sizes= (int[]) clusters.elementAt(i);
                out.write(imageName+"\t");
                out.write((index+1)+"\t");
                out.write((i+1)+"\t");
                out.write(sizes[0]+"\t");   //size of line cluster 1
                out.write(sizes[1]+"\t");   //size of line cluster 2
                out.write(sizes[2]+"\t");   //intersecting line number in line cluster 1
                out.write(sizes[3]+"\t");   //intersecting line number in line cluster 2
                out.write(sizes[4]+"\n");   //intersection made by two clusters
            }
            out.close();
        }
        catch (IOException e) {
            System.err.println("io error: "+e);
        }

    }
/*
    public static class Analysis {
        public static void main(String[] args) {
            if (args.length>0) {
                directory=args[0];
                System.err.println("directory name = "+directory);
            }

            Vector data= new Vector();
            int image_size=0;
            try {
                BufferedReader in= new BufferedReader (
                    new FileReader(directory+"/statics_inter_clusters.rst"));
                String str;
                String imageName;
                int imageIndex;
                int clusterIndex;
                int clusterSize1;
                int clusterSize2;
                int interLines1;
                int interLines2;
                int maxInterLine;
                int interSum;
                boolean satisfy;
                double interLinesNor1;
                double interLinesNor2;
                double maxInterLinesNor;
                while ((str=in.readLine())!=null) {
                    StringTokenizer stn= new StringTokenizer(str,"\t");
                    if (stn.countTokens()<=1) continue;
                    //read the data
                    imageName= stn.nextToken();
                    imageIndex= Integer.parseInt(stn.nextToken());
                    clusterIndex= Integer.parseInt(stn.nextToken());
                    clusterSize1= Integer.parseInt(stn.nextToken());
                    clusterSize2= Integer.parseInt(stn.nextToken());
                    interLines1= Integer.parseInt(stn.nextToken());
                    interLines2= Integer.parseInt(stn.nextToken());
                    maxInterLine= Integer.parseInt(stn.nextToken());
                    interSum= Integer.parseInt(stn.nextToken());
                    satisfy= Boolean.getBoolean(stn.nextToken());
                    interLinesNor1= Double.valueOf(stn.nextToken()).doubleValue();
                    interLinesNor2= Double.valueOf(stn.nextToken()).doubleValue();
                    maxInterLinesNor= Double.valueOf(stn.nextToken()).doubleValue();

                    int[] clusterInfo= {
                        imageIndex,
                        clusterIndex,
                        maxInterLine,
                        (int)(maxInterLinesNor*100)
                    };
                    if (imageIndex>image_size) image_size=imageIndex;
                    data.addElement(clusterInfo);
                }

/*
                out.write(imageName+"\t\n");
                for (int i=0;i<clusters.size();i++) {
                    int[] sizes= (int[]) clusters.elementAt(i);
                    out.write(imageName+"\t");
                    out.write((index+1)+"\t");
                    out.write((i+1)+"\t");
                    out.write(sizes[0]+"\t");
                    out.write(sizes[1]+"\t");
                    out.write(sizes[2]+"\t");
                    out.write(sizes[3]+"\t");
                    out.write((int)Math.max(sizes[2],sizes[3])+"\t");
                    out.write(sizes[2]+sizes[3]+"\t");
                    out.write(((sizes[2]>=12||sizes[3]>=12)&&(sizes[2]+sizes[3]>=20))+"\t");
                    out.write((double)sizes[2]/sizes[0]+"\t");
                    out.write((double)sizes[3]/sizes[1]+"\t");
                    out.write(Math.max((double)sizes[2]/sizes[0],
                        (double)sizes[3]/sizes[1])+"\n");
                }
//
                in.close();
            }
            catch (IOException e) {
                System.err.println("io error: "+e);
            }

            if (image_size==0) return;
            int[][] inter_interNor= new int[101][101];
            boolean[] image_satisfy= new boolean[image_size];
            for (int i=0;i<=100;i+=5) {
                for (int j=0;j<=100;j+=5) {
                    for (int k=0;k<image_size;k++) image_satisfy[k]=false;
                    for (int k=0;k<data.size();k++) {
                        int[] cluster_info= (int[]) data.elementAt(k);
                        if (cluster_info[2]<i||cluster_info[3]<j) continue;
                        image_satisfy[cluster_info[0]-1]=true;
                    }
                    int count=0;
                    for (int k=0;k<image_size;k++) if (image_satisfy[k]) count++;
                    inter_interNor[i][j]=count;
                }
            }
            OutputResult.outputResult("inter_interNor", inter_interNor);
        }
    }
*/
    public static void main(String[] args)
    {
        if (args.length>0) {
            directory=args[0];
        }
        System.err.println("directory name = "+directory);
        if (args.length>1) {
            outFile=args[1];
        }
        else {
            outFile= "statics_inter_clusters.rst";
        }
        System.err.println("output file name = " + outFile);

        //String images[]= yi.util.io.File.getFileNames(directory);
        String images[]= yi.util.io.File.getImagesNameRecursively(directory);
        System.err.println("Image#="+images.length);

        CSOSSM_br br= new CSOSSM_br();
        CSOSSM.TEST= true;
        java.util.Date date= new Date();
        try {
            FileWriter out= new FileWriter("statics.rst");
            for (int i=0;i<images.length;i++) {
                imageName= images[i];
                System.out.println();
                System.out.println("==== "+imageName+" ==== "+(i+1)+"/"+images.length);
            }
            for (int i=0;i<images.length;i++) {
            //for (int i=1;i<=2;i++) {
                imageName= images[i];
                System.err.println();
                System.err.println("==== "+imageName+" ==== "+(i+1)+"/"+images.length);
                source= yi.util.ImageUtil.readImage(imageName, d);
                if (source==null) continue;

                clusters.removeAllElements();

                int[] result= br.getProcessedPseudoColor(source, d);

                outputClusters(i);

                if (OUTPUTRSTIMG) {
                    String newName;
                    int index=imageName.lastIndexOf(".jpg");
                    if (index<0) index=imageName.lastIndexOf(".JPG");
                    if (index<0) index=imageName.lastIndexOf(".ppm");
                    if (index<0) index=imageName.lastIndexOf(".PPM");
                    if (index>=0) newName= imageName.substring(0,index)+"_rst1.gif";
                    else newName= imageName+"_rst.gif";
                    System.err.println("output: "+newName);
                    yi.util.io.OutputResult.outputGIFImage(newName, result, d.width, d.height);
                    result=null;
                    String newName1;
                    if (index>=0) newName1= imageName.substring(0,index)+"_rst0.gif";
                    else newName1= imageName+"_rst0.gif";
                    System.err.println("output: "+newName1);
                    yi.util.io.OutputResult.outputGIFImage(newName1, rstImg, d.width, d.height);
                }

                out.write(imageName+"\t");
                out.write(maxSize+"\t");
                out.write(maxSizeAfterSpatial+"\t");
                out.write(maxSizeAfterOverlap+"\t");
                out.write(maxInterSize+"\t");
                out.write(maxInterCombineSize+"\t");
                out.write(maxInterCombineSizeR+"\t");
                maxSize=maxSizeAfterSpatial=maxSizeAfterOverlap=0;
                maxInterSize=maxInterCombineSize=maxInterCombineSizeR=0;
                //weighted
                out.write(maxSizeW+"\t");
                out.write(maxSizeAfterSpatialW+"\t");
                out.write(maxSizeAfterOverlapW+"\t");
                out.write(maxInterSizeW+"\t");
                out.write(maxInterCombineSizeW+"\t");
                out.write(maxInterCombineSizeWR+"\n");
                maxSizeW=maxSizeAfterSpatialW=maxSizeAfterOverlapW=0;
                maxInterSizeW=maxInterCombineSizeW=maxInterCombineSizeWR=0;
                /*
                //arguments
                System.err.println("maxSize="+maxSize);
                System.err.println("maxSizeAfterSpatial="+maxSizeAfterSpatial);
                System.err.println("maxSizeAfterOverlap="+maxSizeAfterOverlap);
                System.err.println("maxInterSize="+maxInterSize);
                System.err.println("maxInterCombineSize="+maxInterCombineSize);
                System.err.println("maxInterCombineSize="+maxInterCombineSizeR);
                maxSize=maxSizeAfterSpatial=maxSizeAfterOverlap=0;
                maxInterSize=maxInterCombineSize=maxInterCombineSizeR=0;
                //weighted
                System.err.println("maxSizeW="+maxSizeW);
                System.err.println("maxSizeAfterSpatialW="+maxSizeAfterSpatialW);
                System.err.println("maxSizeAfterOverlapW="+maxSizeAfterOverlapW);
                System.err.println("maxInterSizeW="+maxInterSizeW);
                System.err.println("maxInterCombineSizeW="+maxInterCombineSizeW);
                System.err.println("maxInterCombineSizeW="+maxInterCombineSizeWR);
                maxSizeW=maxSizeAfterSpatialW=maxSizeAfterOverlapW=0;
                maxInterSizeW=maxInterCombineSizeW=maxInterCombineSizeWR=0;
                //*/

                source=null;
                rstImg=null;
                System.gc();
                java.util.Date date2= new Date();
                System.err.println("time elapse: "+(date2.getTime()-date.getTime())/1000.0+"/"
                    +(date2.getTime()-date.getTime())/1000.0/(i+1));
            }

            out.close();
        }
        catch (IOException e) {
            System.err.println("io error: "+e);
        }

        System.err.println();
        System.err.println("Done!");
        System.exit(0);
    }
}