package xeij;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.List;           // これを明示的に追加

import purejavahidapi.*;

public class ITX68LED {
  
  public static HidDevice itxDevice = null;
  public static final long ITX_CHECK_SPAN = XEiJ.TMR_FREQ / 20;  //一定時間。50ms
  private static int ejectPressCount0 = 0;
  private static int ejectPressCount1 = 0;
  private static boolean isPuressEject0 = false; 
  private static boolean isPuressEject1 = false; 
  private static int blinkCounter0 = 0; 
  private static int blinkCounter1 = 0; 
  private static int lastLed = -1;

  private static final TickerQueue.Ticker itxCheckTicker = new TickerQueue.Ticker () {
    @Override protected void tick () {
      byte b = (byte)0x80;
      // DRIVE 0
      if (FDC.fdcUnitArray[0].fduIsInserted()) {
        //メディアあり
        if (!FDC.fdcUnitArray[0].fduPrevented) {
          //イジェクト禁止じゃない
           b |= (byte)(1 << 0); //BUTTON
        }
      } else {
        if (FDC.fdcUnitArray[0].fduBlinking) {
          if (blinkCounter0++ > 15) {
            b |= (byte)(1 << 0); //BUTTON
            if (blinkCounter0 == 30) {
              blinkCounter0 = 0;
            }
          }
        } else {
          blinkCounter0 = 0;
        }
      }
      switch (FDC.fdcUnitArray[0].fduMotorStatus) {
      default:
      if (FDC.fdcUnitArray[0].fduIsInserted()) {
          b |= (byte)(1 << 3); //緑
        }
        break;
      case 5: //FDU_MOTOR_RUNNING:  //モータ動作中(レディ)。イジェクト不可
        b |= (byte)(1 << 2); //赤
        break;
      }
      // DRIVE 1
      if (FDC.fdcUnitArray[1].fduIsInserted()) {
        //メディアあり
        if (!FDC.fdcUnitArray[1].fduPrevented) {
          //イジェクト禁止じゃない
           b |= (byte)(1 << 1); //BUTTON
        }
      } else {
        if (FDC.fdcUnitArray[1].fduBlinking) {
          if (blinkCounter1++ > 15) {
            b |= (byte)(1 << 1); //BUTTON
            if (blinkCounter1 == 30) {
              blinkCounter1 = 0;
            }
          }
        } else {
          blinkCounter1 = 0;
        }
      }
      switch (FDC.fdcUnitArray[1].fduMotorStatus) {
      default:
      if (FDC.fdcUnitArray[1].fduIsInserted()) {
          b |= (byte)(1 << 5); //緑
        }
        break;
      case 5: //FDU_MOTOR_RUNNING:  //モータ動作中(レディ)。イジェクト不可
        b |= (byte)(1 << 4); //赤
        break;
      }
      if (lastLed != b) {
        lastLed = b;
        byte[] q = new byte[8];
        q[0] = b;
        itxDevice.setOutputReport((byte)0, q, q.length);
      }
      // DRIVE 0
      if (isPuressEject0) {
        if (ejectPressCount0++ == 0) {
          //Eject
          FDC.fdcUnitArray[0].eject();
        } else if (ejectPressCount0 == 40) {
          //ForceEject
          FDC.fdcUnitArray[0].allow();
          FDC.fdcUnitArray[0].eject();
        }
      } else {
        ejectPressCount0 = 0;
      }
      // DRIVE 1
      if (isPuressEject1) {
        if (ejectPressCount1++ == 0) {
          //Eject
          FDC.fdcUnitArray[1].eject();
        } else if (ejectPressCount1 == 40) {
          //ForceEject
          FDC.fdcUnitArray[1].allow();
          FDC.fdcUnitArray[1].eject();
        }
      } else {
        ejectPressCount1 = 0;
      }
      TickerQueue.tkqAdd (itxCheckTicker, XEiJ.mpuClockTime + ITX_CHECK_SPAN);
    }
  };

  public static void itxReset () {
    HidDeviceInfo devInfo = null;
    List<HidDeviceInfo> devList = PureJavaHidApi.enumerateDevices ();
   itxDevice = null;
    for (HidDeviceInfo info : devList) {
      if (info.getVendorId () == (short)0x04D8 && info.getProductId () == (short)0xE482) {
        devInfo = info;
        break;
      }
    }
    if (devInfo != null) {
      try {
        itxDevice = PureJavaHidApi.openDevice (devInfo);
      } catch (IOException e) {
        itxDevice = null;
      }
      if (itxDevice != null) {
        itxDevice.setInputReportListener ((source, reportID, data, len) -> {
          switch (data[0]) {
            case 'A': isPuressEject0 = true; break;
            case 'a': isPuressEject0 = false; break;
            case 'B': isPuressEject1 = true; break; 
            case 'b': isPuressEject1 = false; break;
            default:
               break;
          }
        });
        byte[] b = new byte[8];
        b[0] = (byte)0x80;
        itxDevice.setOutputReport ((byte)0, b, b.length);
        TickerQueue.tkqAdd (itxCheckTicker, XEiJ.mpuClockTime + ITX_CHECK_SPAN);
        System.out.printf("ITX68-FDD-LED に接続しました。\n");
      }
    }
    devInfo= null;
  }
};

