package com.device.comm.DGMS.algorithm;


import java.io.PrintStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class ReferenceManager
{
  private List<Reference> refList;
  private Reference curReference = null;
  private  Reference mCurrentReference = null;

  private int kCount = -1;
  
  public ReferenceManager()
  {
    this.refList = new ArrayList();
  }
  
  public Reference getRefValue()
  {
    return this.curReference;
  }
  

  public void addCurrentReference(double value,int index){
    mCurrentReference = new Reference(value,index);
  }

  public void addReference(double value, Date time)
  {
    Reference ref = new Reference(value, time);
    if (!isExist(ref)) {
      this.refList.add(ref);
    }
//    Collections.sort(this.refList, new Comparator()
//    {
//      public int compare(Reference b1, Reference b2)
//      {
//        if (b1.getTime().after(b2.getTime())) {
//          return 1;
//        }
//        return -1;
//      }
//    });
    Collections.sort(this.refList, new Comparator<Reference>() {
      @Override
      public int compare(Reference lhs, Reference rhs) {
        if (lhs.getTime().after(rhs.getTime())) {
          return 1;
        }
        return -1;      }
    });
  }
  
  private boolean isExist(Reference bgRef)
  {
    for (Reference bg : this.refList) {
      if (bg.getTime().getTime() == bgRef.getTime().getTime()) {
        return true;
      }
    }
    return false;
  }
  
  public boolean isCalK()
  {
    if (this.kCount == 0) {
      return true;
    }
    return false;
  }
  
  public boolean isWaitCalK()
  {
    if ((this.kCount == 2) || (this.kCount == 1)) {
      return true;
    }
    return false;
  }
  
  public static void main(String[] args)
  {
    ReferenceManager refManager = new ReferenceManager();
    refManager.addReference(3.0D, StringToDate("2015/12/30 16:00:00"));
    refManager.addReference(1.0D, StringToDate("2015/12/30 14:00:00"));
    refManager.addReference(2.0D, StringToDate("2015/12/30 15:00:00"));
    Reference f = refManager
      .findReference(StringToDate("2015/12/30 14:01:00"));
    if (f != null) {
      System.out.println(f.getValue());
    } else {
      System.out.println("not");
    }
  }
  
  public Reference findReference(Date curTime)
  {
    for (Reference d : this.refList)
    {
      double interval = (curTime.getTime() - d.getTime().getTime()) / 
        60000L;
      if (interval <= 0.0D)
      {
        this.kCount -= 1;
        return null;
      }
      if (interval <= 3.0D)
      {
        this.kCount = 2;
        this.curReference = d;
        return d;
      }
    }
    this.kCount -= 1;
    return null;
  }

  public Reference findReference(int index){
    double interval = (index - mCurrentReference.getIndex())*3;

    if (interval < 0.0D)
    {
      this.kCount -= 1;
      return null;
    }
    if (interval <= 3.0D)
    {
      this.kCount = 2;
      this.curReference = mCurrentReference;
      return mCurrentReference;
    }


    this.kCount -= 1;
    return null;
  }

  
  public static Date StringToDate(String dateStr)
  {
    String formatStr = "yyyy/MM/dd HH:mm:ss";
    DateFormat dd = new SimpleDateFormat(formatStr);
    Date date = null;
    try
    {
      date = dd.parse(dateStr);
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
    return date;
  }
}


