package lt.ktu.gmj.analysis;

import java.awt.Event;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import lt.ktu.gmj.Result;
import lt.ktu.gmj.ResultRepository;
import lt.ktu.gmj.tasks.BuyQuantityByPrice;
import lt.ktu.gmj.tasks.OptimalContractInfo;
import lt.ktu.gmj.tasks.WalrasMarket;
import lt.ktu.gmj.tasks.WalrasModel;
import lt.monarch.charts.Chart;
import lt.monarch.charts.ChartPoint;
import lt.monarch.charts.ChartPointProvider;
import lt.monarch.charts.ChartPointSeries;
import lt.monarch.charts.ConnectedSeries;
import lt.monarch.charts.Grid;
import lt.monarch.charts.InvisibleDot;
import lt.monarch.charts.MathAxisMapper;
import lt.monarch.charts.XAxis;
import lt.monarch.charts.YAxis;
public class WalrasProfit
    extends ResultFrame
    implements ChartPointProvider {

  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
public int GetDomN(int projection) {
    if (projection == 0) {
      return 1;
    }
    if (projection == 1) {
      return 4;
    }
    if (projection == 2) {
      return 2;
    }
    if (projection == 3) {
      return 5;
    }
    if (projection == 4) {
      return 6;
    }
    if (projection == 5) {
          return 7;
        }
        if (projection == 6) {
      return 6;
    }
    if (projection == 7) {
          return 7;
        }
        if (projection == 8) {
              return 6;
            }
            if (projection == 9) {
                  return 7;
                }
                if (projection == 10) {
              return 6;
            }
            if (projection == 11) {
                  return 7;
                }


    System.out.println("ERROR: GetDomN");
    return 0;
  }

  public double GetXMin(int projection) {
    if (projection == 0) {
      return getWalrasModel().domain().min[2];
    }
    if (projection == 1) {
      return getWalrasModel().domain().min[3];
    }
    if (projection == 2) {
      return getWalrasModel().domain().min[0];
    }
    if (projection == 3) {
      return getWalrasModel().domain().min[1];
    }
    if (projection == 4) {
     return 0.0;
   }
   if (projection == 5) {
        return 0.0;
      }
   if (projection == 6) {
        return 0.0;
      }
   if (projection == 7) {
        return 0.0;
      }
   if (projection == 8) {
        return 0.0;
      }
   if (projection == 9) {
        return 0.0;
      }
  if (projection == 10) {
        return 0.0;
      }
  if (projection == 11) {
        return 0.0;
      }

    return 0;
  }

  public double GetXMax(int projection) {
    if (projection == 0) {
      return getWalrasModel().domain().max[2];
    }
    if (projection == 1) {
      return getWalrasModel().domain().max[3];
    }
    if (projection == 2) {
      return getWalrasModel().domain().max[0];
    }
    if (projection == 3) {
      return getWalrasModel().domain().max[1];
    }
    if (projection == 4) {
      return getWalrasModel().GetB2() ;
    }
    if (projection == 5) {
      return getWalrasModel().GetB1();
        }
    if (projection == 6) {
      return getWalrasModel().GetB2() ;
    }
    if (projection == 7) {
      return getWalrasModel().GetB1();
    }
    if (projection == 8) {
      return getWalrasModel().GetB2() ;
    }
    if (projection == 9) {
      return getWalrasModel().GetB1();
    }
    if (projection == 10) {
      return getWalrasModel().GetB2() ;
    }
    if (projection == 11) {
      return getWalrasModel().GetB1();
    }
    return 0;
  }

  public double GetXStep(int projection) {
    if (projection == 0) {
      return (getWalrasModel().domain().max[2] -
              getWalrasModel().domain().min[2]) /
          (100 / getWalrasModel().Precision_Analyzer);
    }
    if (projection == 1) {
      return (getWalrasModel().domain().max[3] -
              getWalrasModel().domain().min[3]) /
          (100 / getWalrasModel().Precision_Analyzer);
    }
    if (projection == 2) {
      return (getWalrasModel().domain().max[0] -
              getWalrasModel().domain().min[0]) /
          (100 / getWalrasModel().Precision_Analyzer);
    }
    if (projection == 3) {
      return (getWalrasModel().domain().max[1] -
              getWalrasModel().domain().min[1]) /
          (100 / getWalrasModel().Precision_Analyzer);
    }
    if (projection == 4) {
      return (getWalrasModel().GetB2()) /
          (100 / getWalrasModel().Precision_Analyzer);
    }
    if (projection == 5) {
          return (getWalrasModel().GetB1()) /
              (100 / getWalrasModel().Precision_Analyzer);
        }
    if (projection == 6) {
              return (getWalrasModel().GetB2()) /
                  (100 / getWalrasModel().Precision_Analyzer);
            }
   if (projection == 7) {
     return (getWalrasModel().GetB1()) /
         (100 / getWalrasModel().Precision_Analyzer);
   }
   if (projection == 8) {
     return (getWalrasModel().GetB2()) /
         (100 / getWalrasModel().Precision_Analyzer);
   }
   if (projection == 9) {
     return (getWalrasModel().GetB1()) /
         (100 / getWalrasModel().Precision_Analyzer);
   }
   if (projection == 10) {
     return (getWalrasModel().GetB2()) /
         (100 / getWalrasModel().Precision_Analyzer);
   }
   if (projection == 11) {
     return (getWalrasModel().GetB1()) /
         (100 / getWalrasModel().Precision_Analyzer);
   }

    return 0;

  }

  public double GetYMin() {
    if (profy.length < 1) {
      return 0;
    }
    int i;
    double min = 999999;
    for (i = 0; i < profy.length; i++) {
      if (profy[i] < min) {
        min = profy[i];
      }
    }
    return min;
  }

  public double GetYMax() {
    if (profy.length < 1) {
      return 1;
    }
    int i;
    double max = -999999;
    for (i = 0; i < profy.length; i++) {
      if (profy[i] > max) {
        max = profy[i];
      }
    }
    return max;
  }

  // smooth using Wiener filter
  public void WienerFilter() {
    double b[] = new double[n];
    double c[] = new double[n];
    double r;
    double s;
    s = (new Double(st.getText())).doubleValue();

    double bb = 1; // (0.3)
    b[0] = 1; // (0.3)
    for (int i = 1; i < n; i++) {
      r = Math.abs(x_in[i] - x_in[i - 1]);
      if (s == 0) {
        System.out.println("division by 0. (1)");
      }
      b[i] = (s * b[i - 1] + r * bb) / s; // (0.5)
      bb = bb + b[i]; // (0.5)
    }

    double cc = 0;
    c[n - 1] = 1; // (0.6)
    for (int i = n - 2; i >= 0; i--) {
      r = Math.abs(x_in[i] - x_in[i + 1]);
      cc = cc + c[i + 1]; // (0.7)
      if (s == 0) {
        System.out.println("division by 0. (2)");
      }
      c[i] = (s * c[i + 1] + r * cc) / s;
    }

    double sby, sb, scy, sc, tmp;
    for (int i = 0; i < n; i++) {
      sby = 0;
      sb = 0;
      scy = 0;
      sc = 0;
      for (int j = 0; j <= i; j++) {
        sby = sby + b[j] * y_in[j];
        sb = sb + b[j];
      }
      for (int j = i + 1; j < n; j++) {
        scy = scy + c[j] * y_in[j];
        sc = sc + c[j];
      }

      tmp = sb + b[i] * sc / c[i];
      if (tmp == 0) {
        System.out.println("division by 0. (3)");
      }
      if (c[i] == 0) {
        System.out.println("division by 0. (4)");
      }
      y_out[i] = (sby + b[i] * scy / c[i]) / tmp; // (0.2)

    }
  }

  public synchronized void WienerFilterCmd() {
    n = Math.min(profx.length, profy.length);
    y_in = new double[n];
    y_out = new double[n];
    x_in = new double[n];

    for (int i = 0; i < n; i++) {
      x_in[i] = profx[i];
    }
    for (int i = 0; i < n; i++) {
      y_in[i] = profy[i];
    }
    for (int i = 0; i < n; i++) {
      original_profy[i] = profy[i];

    }
    WienerFilter();

    for (int i = 0; i < n; i++) {
      profy[i] = y_out[i];

    }
    only_refresh = 1;
    setupChart();
    only_refresh = 0;
  }

  // smooth using low pass filter
  public void SmoothProfit() {
    for (int i = 0; i < n; i++) {
      double t1, t2, t3, nt2;
      t1 = y_in[i];
      t2 = y_in[i];
      t3 = y_in[i];
      if (i - 1 > 0) {
        t1 = y_in[i - 1];
      }
      if (i + 1 < n) {
        t3 = y_in[i + 1];
      }
      nt2 = (t1 + t2 + t3) / 3;
      y_out[i] = nt2;
    }
  }

  public synchronized void SmoothProfitCmd() {
    n = Math.min(profx.length, profy.length);
    y_in = new double[n];
    y_out = new double[n];
    x_in = new double[n];

    for (int i = 0; i < n; i++) {
      x_in[i] = profx[i];
    }
    for (int i = 0; i < n; i++) {
      y_in[i] = profy[i];
    }
    for (int i = 0; i < n; i++) {
      original_profy[i] = profy[i];

    }
    SmoothProfit();

    for (int i = 0; i < n; i++) {
      profy[i] = y_out[i];

    }
    only_refresh = 1;
    setupChart();
    only_refresh = 0;
  }

  public synchronized void Undo() {
    for (int i = 0; i < original_profy.length; i++) {
      profy[i] = original_profy[i];
    }
    only_refresh = 1;
    setupChart();
    only_refresh = 0;
  }

  public WalrasProfit() {
    super("Server Profit");
    System.out.println("Analyser Walras Profit. ( Vytautas Perlibakas, 1999, Kaunas\n Modified By Povilas Treigys EISm-3 VGTU Vilnius 2004 )");
    setWalrasModel(WalrasModel.getWalrasModel());
    only_refresh = 0;
    chart = new Chart();
    getContentPane().add("Center", chart);
    projectionChoice = new JComboBox();
    projectionChoice.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        projection = projectionChoice.getSelectedIndex();
        setupChart();
      }
    });
    getContentPane().add("South", projectionChoice);
    JPanel panel = new JPanel();
    JPanel panel11 = new JPanel();
    JPanel panel12 = new JPanel();
    JPanel panel13 = new JPanel();
    JPanel panel2 = new JPanel();
    panel.setLayout(new GridLayout(3, 1));
    panel11.setLayout(new GridLayout(1, 0, 0, 0));
    panel12.setLayout(new GridLayout(1, 0, 0, 0));
    panel13.setLayout(new GridLayout(1, 0, 0, 0));
    panel2.setLayout(new GridLayout(10, 1));
    panel.add("North", panel11);
    panel.add("North", panel12);
    panel.add("North", panel13);
    btn_smooth = new JButton("Smooth");
    btn_smooth.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        SmoothProfitCmd();
      }
    });
    panel2.add("North", btn_smooth);
    btn_wiener = new JButton("Wiener");
    btn_wiener.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        WienerFilterCmd();
      }
    });
    panel2.add("North", btn_wiener);
    btn_undo = new JButton("Undo");
    btn_undo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Undo();
      }
    });
    panel2.add("North", btn_undo);
    btn_refresh = new JButton("Refresh");
    btn_refresh.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        setupChart();
      }
    });
    panel2.add("North", btn_refresh);
    diff = new JLabel("0");
    profit1 = new JLabel("0");
    profit2 = new JLabel("0");
    bestY1 = new JLabel("0");
    bestP1 = new JLabel("0");
    bestY2 = new JLabel("0");
    bestP2 = new JLabel("0");
    st = new JTextField("10", 6);
    diff.setText(Double.toString(getWalrasModel().getBestParams().Diff));
    profit1.setText(Double.toString(getWalrasModel().getBestParams().U1));
    profit2.setText(Double.toString(getWalrasModel().getBestParams().U2));
    bestY1.setText(Double.toString(getWalrasModel().getBestParams().Y1));
    bestP1.setText(Double.toString(getWalrasModel().getBestParams().P1));
    bestY2.setText(Double.toString(getWalrasModel().getBestParams().Y2));
    bestP2.setText(Double.toString(getWalrasModel().getBestParams().P2));
    panel11.add("North", new JLabel("F(x) difference = "));
    panel11.add("North", diff);
    panel11.add("North", new JLabel("Smoothing = "));
    panel11.add("North", st);
    panel12.add("North", new JLabel("1 - st server profit = "));
    panel12.add("North", profit1);
    panel12.add("North", new JLabel("2 - nd server profit = "));
    panel12.add("North", profit2);
    panel13.add("North", new JLabel("Y_1 = "));
    panel13.add("North", bestY1);
    panel13.add("North", new JLabel("P_1 = "));
    panel13.add("North", bestP1);
    panel13.add("North", new JLabel("Y_2 = "));
    panel13.add("North", bestY2);
    panel13.add("North", new JLabel("P_2 = "));
    panel13.add("North", bestP2);
    getContentPane().add("North", panel);
    getContentPane().add("East", panel2);

  }

  protected synchronized void setupChart() {
    Vector seriesList = new Vector();
    MathAxisMapper yMapper = new MathAxisMapper();
    MathAxisMapper xMapper = new MathAxisMapper();
    XAxis axisX = new XAxis(xMapper);
    YAxis axisY = new YAxis(yMapper);
    series = new ConnectedSeries(this, axisX, axisY);
    Grid grid = new Grid(axisX, axisY);

    String ylabel[] = {
        "U1","U2","U1","U2","X12","X21","X12","X21","X12","X21","X12","X21"};
    axisY.setLabel(ylabel[projection]);
    String xlabel[] = {
        "Y1","Y2","P1","P2","P2","P1","Y1","Y2","P1","P2","Y2","Y1"};
    axisX.setLabel(xlabel[projection]);

    if (only_refresh == 0) {
      profx = new double[(int)(100 / getWalrasModel().Precision_Analyzer )+1];
      profy = new double[(int)(100 / getWalrasModel().Precision_Analyzer )+1];
      original_profy = new double[(int)(100 / getWalrasModel().Precision_Analyzer)+1];
      CalculateOutput();
    }

    cur_point_nr = 0;

    double tmpymin=0, tmpymax=0;
    if ((projection!=4)||(projection!=5)||(projection!=6)||(projection!=7)||(projection!=8)||(projection!=9)||(projection!=10)||(projection!=11)){
      tmpymin = GetYMin();
      tmpymax = GetYMax();
    }

    if(projection==4) {
          tmpymin = 0;
          tmpymax = getWalrasModel().GetB2() ;
    }
        if(projection==5) {
         tmpymin = 0;
         tmpymax = getWalrasModel().GetB1() ;
       }
       if(projection==6) {
         tmpymin = 0;
         tmpymax = getWalrasModel().GetB2() ;
       }
       if(projection==7) {
         tmpymin = 0;
         tmpymax = getWalrasModel().GetB1() ;
       }
       if(projection==8) {
         tmpymin = 0;
         tmpymax = getWalrasModel().GetB2() ;
       }
       if(projection==9) {
         tmpymin = 0;
         tmpymax = getWalrasModel().GetB1() ;
       }
       if(projection==10) {
         tmpymin = 0;
         tmpymax = getWalrasModel().GetB2() ;
       }
       if(projection==11) {
         tmpymin = 0;
         tmpymax = getWalrasModel().GetB1() ;
       }

    if (tmpymin == tmpymax) {
      tmpymin = tmpymin - 1;
      tmpymax = tmpymax + 1;
    }
    yMapper.range.setRange(tmpymin, tmpymax);

    if ((projection!=4)||(projection!=5)||(projection!=6)||(projection!=7)||(projection!=8)||(projection!=9)||(projection!=10)||(projection!=11))
    {
      xMapper.range.setRange(GetXMin(projection), GetXMax(projection));
    }

    if (projection == 4)
    {
      xMapper.range.setRange(GetXMin(3), GetXMax(3));
    }
    if (projection == 5)
    {
      xMapper.range.setRange(GetXMin(2), GetXMax(2));
    }
    if (projection == 6)
    {
      xMapper.range.setRange(GetXMin(1), GetXMax(1));
    }
    if (projection == 7)
    {
      xMapper.range.setRange(GetXMin(0), GetXMax(0));
    }
    if (projection == 8)
    {
      xMapper.range.setRange(GetXMin(3), GetXMax(3));
    }
    if (projection == 9)
    {
      xMapper.range.setRange(GetXMin(2), GetXMax(2));
    }
    if (projection == 10)
    {
      xMapper.range.setRange(GetXMin(1), GetXMax(1));
    }
    if (projection == 11)
    {
      xMapper.range.setRange(GetXMin(0), GetXMax(0));
    }


    seriesList.addElement(series);
    axisX.prepare();
    axisY.prepare();

    chart.setup(seriesList);
    chart.setLeftAxis(axisY);
    chart.setXAxis(axisX);
    chart.setGrid(grid);
    chart.repaint();
  }

  public int PointCnt() {
    //getWalrasModel().pointnum[0]=(int)(100 / getWalrasModel().Precision_Analyzer )+1;
    //getWalrasModel().pointnum[1]=(int)(100 / getWalrasModel().Precision_Analyzer )+1;
    //return getWalrasModel().pointnum[projection];
    getWalrasModel().pointnum[0] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer) +
        1;
    getWalrasModel().pointnum[1] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer) +
        1;
    getWalrasModel().pointnum[2] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer) +
        1;
    getWalrasModel().pointnum[3] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer) +
        1;
    getWalrasModel().pointnum[4] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer)+1;
    getWalrasModel().pointnum[5] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer)+1;
    getWalrasModel().pointnum[6] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer)+1;
    getWalrasModel().pointnum[7] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer)+1;
    getWalrasModel().pointnum[8] = (int) (100 /
                                          getWalrasModel().Precision_Analyzer)+1;
    getWalrasModel().pointnum[9] = (int) (100 /
                                        getWalrasModel().Precision_Analyzer)+1;
  getWalrasModel().pointnum[10] = (int) (100 /
                                        getWalrasModel().Precision_Analyzer)+1;
  getWalrasModel().pointnum[11] = (int) (100 /
                                        getWalrasModel().Precision_Analyzer)+1;

    return getWalrasModel().pointnum[projection];

  }

  public int pointCount() {
    return Math.min(profx.length, profy.length);
  }

  public void CalculateOutput() 
  {
    double ptx[];
    ptx = new double[8];
    ptx[0] = getWalrasModel().getBestParams().X11;
    ptx[1] = getWalrasModel().getBestParams().Y1;
    ptx[2] = getWalrasModel().getBestParams().P1;
    ptx[3] = getWalrasModel().getBestParams().X22;
    ptx[4] = getWalrasModel().getBestParams().Y2;
    ptx[5] = getWalrasModel().getBestParams().P2;
    ptx[6] = getWalrasModel().getBestParams().X12;
    ptx[7] = getWalrasModel().getBestParams().X21;
   // buyQuantity = new BuyQuantityByPrice(modelValues);
    if (projection == 0 || projection == 1 || projection == 2 || projection == 3)
    {
    	int j = 0;
    	for (double i = GetXMin(projection); i <= GetXMax(projection); i += GetXStep(projection)) 
    	{
    		ptx[GetDomN(projection)] = i;
                
                //public OptimalContractInfo Get(double P1, double Y1, double P2, double Y2)
                //OptimalContractInfo bq = buyQuantity.Get(ptx[2], ptx[1], ptx[5], ptx[4]);
    		ptx[6] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X12;
                ptx[7] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X21;
                //igor idea
                ptx[0] = getWalrasModel().modelValues.B1 - getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X12;
                ptx[3] = getWalrasModel().modelValues.B2 - getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X21;
                WalrasMarket market = getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                        ptx[5], ptx[6], ptx[7]);
    		profy[j] = market.GetProfit(projection%2);
    		profx[j] = i;
    		j = j + 1;
    	}
    }
    else if(projection==4)
    {
        int j = 0;
        for (double i = GetXMin(3); i <= GetXMax(3); i += GetXStep(3))
        {
        	ptx[5] = i; //P2
        	getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                        ptx[5], ptx[6], ptx[7]);

        	profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X12;
        	profx[j] = i;
        	j = j + 1;
        }
    }
    else if(projection==5)
    {
    	int j = 0;
    	for (double i = GetXMin(2); i <= GetXMax(2); i += GetXStep(2))
    	{
    		ptx[2] = i; //P1
    		getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                        ptx[5], ptx[6], ptx[7]);
    		profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X21;
    		profx[j] = i;
    		j = j + 1;
    	}
    }
    else if(projection==6)
    {
    	int j = 0;
        for (double i = GetXMin(0); i <= GetXMax(0); i += GetXStep(0)) 
        {
        	ptx[1] = i; //Y1
        	getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                        ptx[5], ptx[6], ptx[7]);
        	profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X12;
        	profx[j] = i;
        	j = j + 1;
        }
    }
    else if(projection==7)
    {
    	int j = 0;
    	for (double i = GetXMin(1); i <= GetXMax(1); i += GetXStep(1)) 
    	{
    		ptx[4] = i; //Y2
    		getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                        ptx[5], ptx[6], ptx[7]);
    		profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X21;
    		profx[j] = i;
    		j = j + 1;
    	}
    }
    else if(projection==8)
    {
    	int j = 0;
        for (double i = GetXMin(2); i <= GetXMax(2); i += GetXStep(2))
        {
        	ptx[2] = i; //P1
            getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                            ptx[5], ptx[6], ptx[7]);
            profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X12;
            profx[j] = i;
            j = j + 1;
        }
    }
    else if(projection==9)
    {
    	int j = 0;
        for (double i = GetXMin(3); i <= GetXMax(3); i += GetXStep(3))
        {
        	ptx[5] = i; //P2
            getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                            ptx[5], ptx[6], ptx[7]);
            profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X21;
            profx[j] = i;
            j = j + 1;
        }
    }
    else if(projection==10)
    {
    	int j = 0;
        for (double i = GetXMin(1); i <= GetXMax(1); i += GetXStep(1)) 
        {
        	ptx[4] = i; //Y2
            getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                            ptx[5], ptx[6], ptx[7]);
            profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X12;
            profx[j] = i;
            j = j + 1;
        }
    }
    else if(projection==11)
    {
    	int j = 0;
        for (double i = GetXMin(0); i <= GetXMax(0); i += GetXStep(0)) 
        {
        	ptx[1] = i; //Y1
            getWalrasModel().ProcessServers(ptx[0], ptx[1], ptx[2], ptx[3], ptx[4],
                                            ptx[5], ptx[6], ptx[7]);
            profy[j] = getWalrasModel().getBuyQuantity().Get(ptx[2], ptx[1], ptx[5], ptx[4]).X21;
            profx[j] = i;
            j = j + 1;
        }
    }
  }


  public ChartPoint point(int i) {
    cur_point_nr = i;
    return pointFromResult(results.resultAt(0));
  }

  protected ChartPoint pointFromResult(Result r) {
    double tmpx, tmpy;

    tmpx = profx[cur_point_nr];
    tmpy = profy[cur_point_nr];

    ChartPoint pt = new InvisibleDot(new Double(tmpx), new Double(tmpy));
    return pt;
  }

  public synchronized void newResult(ResultRepository n) {
    curriter.setText(Integer.toString(getWalrasModel().Iteration_current));
  }

  public void newProgressResult(ResultRepository n) {
    diff.setText(Double.toString(getWalrasModel().getBestParams().Diff));
    profit1.setText(Double.toString(getWalrasModel().getBestParams().U1));
    profit2.setText(Double.toString(getWalrasModel().getBestParams().U2));
    bestY1.setText(Double.toString(getWalrasModel().getBestParams().Y1));
    bestP1.setText(Double.toString(getWalrasModel().getBestParams().P1));
    bestY2.setText(Double.toString(getWalrasModel().getBestParams().Y2));
    bestP2.setText(Double.toString(getWalrasModel().getBestParams().P2));
    setupChart();

  }

  public void finalResult(ResultRepository n) {
    newProgressResult(n);
  }

  public synchronized void prepare(ResultRepository n) {
    super.prepare(n);
    /*
    String dim[] = {
        "1-st server profit (y1)",
        "2-nd server profit (y2)",
        "1-st server profit (p1)",
        "2-nd server profit (p2)",
        "B2 server buy qt. (X12(P2))",
        "B1 server buy qt. (X21(P1))",
        "B2 server buy qt. (X12(Y1))",
        "B1 server buy qt. (X21(Y2))",
        "B2 server buy qt. (X12(P1))",
        "B1 server buy qt. (X21(P2))",
        "B2 server buy qt. (X12(Y2))",
        "B1 server buy qt. (X21(Y1))"
        */
     String dim[] = {
        "1-st server profit (y1)",
        "2-nd server profit (y2)",
        "1-st server profit (p1)",
        "2-nd server profit (p2)",
        "1 server buy from 2 qt. (X12(P2))",
        "2 server buy from 1 qt. (X21(P1))",
        "1 server buy from 2 qt. (X12(Y1))",
        "2 server buy from 1 qt. (X21(Y2))",
        "1 server buy from 2 qt. (X12(P1))",
        "2 server buy from 1 qt. (X21(P2))",
        "1 server buy from 2 qt. (X12(Y2))",
        "2 server buy from 1 qt. (X21(Y1))"
    };

    for (int i = 0; i < dim.length; i++) {
      projectionChoice.addItem(dim[i]);
    }
    setupChart();
    results.addObserver(this);
    show();
  }

  public synchronized void newLog(ResultRepository n) {
    chart.repaint();
  }

  public boolean action(Event evt, Object arg) {
    if (evt.target == projectionChoice) {
      projection = projectionChoice.getSelectedIndex();
      setupChart();
    }
    if (evt.target == btn_smooth) {
      SmoothProfitCmd();
    }
    if (evt.target == btn_wiener) {
      WienerFilterCmd();
    }
    if (evt.target == btn_undo) {
      Undo();
    }
    if (evt.target == btn_refresh) {
      setupChart();
    }

    return false;
  }

  private WalrasModel getWalrasModel() {
    return gWalrasModel;
  }

  private void setWalrasModel(WalrasModel igWalrasModel) {
    gWalrasModel = igWalrasModel;
  }

  public JButton btn_smooth;
  public JButton btn_wiener;
  public JButton btn_undo;
  public JButton btn_refresh;
  public double y_in[];
  public double x_in[];
  public double y_out[];
  public int n;
  public int only_refresh;
  public int cur_point_nr;
  public double profx[];
  public double profy[];
  public double original_profy[];
  public JTextField st;
  public JLabel control1;
  public JLabel control2;
  public JLabel clserved1;
  public JLabel clserved2;
  public JLabel clgone;
  public JLabel bestiter;
  public JLabel curriter;
  public JLabel diff;
  public JLabel profit1;
  public JLabel profit2;
  public JLabel v1;
  public JLabel v2;
  public JLabel bestX11;
  public JLabel bestY1;
  public JLabel bestP1;
  public JLabel bestX22;
  public JLabel bestY2;
  public JLabel bestP2;
  public JLabel bestX12;
  public JLabel bestX21;
  private static final String CMD_SMOOTH = "Smooth";
  private static final String CMD_WIENER = "Wiener";
  private static final String CMD_UNDO = "Undo";
  private static final String CMD_REFRESH = "Refresh";
  protected Chart chart;
  protected int projection;
  protected ChartPointSeries series;
  protected JComboBox projectionChoice;
  private WalrasModel gWalrasModel;
  //public BuyQuantityByPrice buyQuantity = null;

}
