#include "pending.h"
#include <QPalette>

Pending::Pending(Ui_MainWindow *mainWindow, Ui_ROIWindow *roiWindow,
                 CMOS *sensor) {
  this->mainWindow = mainWindow;
  this->roiWindow = roiWindow;
  this->sensor = sensor;
  this->enabled = false;
  this->ROIpendingCount = 0;

  this->origStyle = this->mainWindow->exposureSpin->palette();
  this->pendingStyle = this->origStyle;
  this->pendingStyle.setColor(QPalette::Active, QPalette::Text,
                              QColor(255, 0, 0));
  this->pendingStyle.setColor(QPalette::Active, QPalette::ButtonText,
                              QColor(255, 0, 0));
  this->pendingStyle.setColor(QPalette::Active, QPalette::Button,
                              QColor(255, 0, 0));
  this->pendingStyle.setColor(QPalette::Active, QPalette::Window,
                              QColor(255, 0, 0));
}

void Pending::ROI_widthChanged(int width) {
  width = width * (this->sensor->getBinning().collum == 0
                       ? 1
                       : this->sensor->getBinning().collum);
  if (width != this->sensor->getROI().width) {
    if (this->roiWindow->widthSpin->palette() != this->pendingStyle) {
      this->ROIpendingCount++;
    }
    this->roiWindow->widthSpin->setPalette(this->pendingStyle);
  } else {
    if (this->roiWindow->widthSpin->palette() != this->origStyle) {
      this->ROIpendingCount--;
    }
    this->roiWindow->widthSpin->setPalette(this->origStyle);
  }
  if (this->ROIpendingCount > 0) {
    this->mainWindow->setROIButton->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->setROIButton->setPalette(this->origStyle);
  }
}
void Pending::ROI_heightChanged(int height) {
  height = height * (this->sensor->getBinning().row == 0
                         ? 1
                         : this->sensor->getBinning().row);
  if (height != this->sensor->getROI().height) {
    if (this->roiWindow->heightSpin->palette() != this->pendingStyle) {
      this->ROIpendingCount++;
    }
    this->roiWindow->heightSpin->setPalette(this->pendingStyle);
  } else {
    if (this->roiWindow->heightSpin->palette() != this->origStyle) {
      this->ROIpendingCount--;
    }
    this->roiWindow->heightSpin->setPalette(this->origStyle);
  }
  if (this->ROIpendingCount > 0) {
    this->mainWindow->setROIButton->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->setROIButton->setPalette(this->origStyle);
  }
}
void Pending::ROI_startXChanged(int startX) {
  startX = startX * (this->sensor->getBinning().collum == 0
                         ? 1
                         : this->sensor->getBinning().collum);
  if (startX != this->sensor->getROI().beginX) {
    if (this->roiWindow->startXSpin->palette() != this->pendingStyle) {
      this->ROIpendingCount++;
    }
    this->roiWindow->startXSpin->setPalette(this->pendingStyle);
  } else {
    if (this->roiWindow->startXSpin->palette() != this->origStyle) {
      this->ROIpendingCount--;
    }
    this->roiWindow->startXSpin->setPalette(this->origStyle);
  }
  if (this->ROIpendingCount > 0) {
    this->mainWindow->setROIButton->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->setROIButton->setPalette(this->origStyle);
  }
}
void Pending::ROI_startYChanged(int startY) {
  startY = startY * (this->sensor->getBinning().row == 0
                         ? 1
                         : this->sensor->getBinning().row);
  if (startY != this->sensor->getROI().beginY) {
    if (this->roiWindow->startYSpin->palette() != this->pendingStyle) {
      this->ROIpendingCount++;
    }
    this->roiWindow->startYSpin->setPalette(this->pendingStyle);
  } else {
    if (this->roiWindow->startYSpin->palette() != this->origStyle) {
      this->ROIpendingCount--;
    }
    this->roiWindow->startYSpin->setPalette(this->origStyle);
  }
  if (this->ROIpendingCount > 0) {
    this->mainWindow->setROIButton->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->setROIButton->setPalette(this->origStyle);
  }
}

void Pending::AECChanged(int state) {
  if ((state >> 1) != this->sensor->getAEC()) {
    this->mainWindow->AECCheck->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->AECCheck->setPalette(this->origStyle);
  }
}
void Pending::AGCChanged(int state) {
  if ((state >> 1) != this->sensor->getAGC()) {
    this->mainWindow->AGCCheck->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->AGCCheck->setPalette(this->origStyle);
  }
}
void Pending::HDRChanged(int state) {
  if ((state >> 1) != this->sensor->getHDR()) {
    this->mainWindow->HDRCheck->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->HDRCheck->setPalette(this->origStyle);
  }
}
void Pending::Flip_RowChanged(int state) {
  if ((state >> 1) != this->sensor->getFlip().row) {
    this->mainWindow->flipRowBox->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->flipRowBox->setPalette(this->origStyle);
  }
}
void Pending::Flip_ColChanged(int state) {
  if ((state >> 1) != this->sensor->getFlip().collum) {
    this->mainWindow->flipColBox->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->flipColBox->setPalette(this->origStyle);
  }
}

void Pending::BrightChanged(int bright) {
  if (bright != this->sensor->getBrightness()) {
    this->mainWindow->brightnessSpin->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->brightnessSpin->setPalette(this->origStyle);
  }
}
void Pending::ExposureChanged(int exposure) {
  if (exposure != static_cast<int>(this->sensor->getExposureTime())) {
    this->mainWindow->exposureSpin->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->exposureSpin->setPalette(this->origStyle);
  }
}
void Pending::GainChanged(double gain) {
  if (static_cast<float>(gain) !=
      this->sensor->gainToReal(this->sensor->getManualGain())) {
    this->mainWindow->gainSpin->setPalette(this->pendingStyle);
  } else {
    this->mainWindow->gainSpin->setPalette(this->origStyle);
  }
}

void Pending::Bin_RowChanged(QString row) {
  if (this->mainWindow->binningLabel->currentText().compare("Binning") == 0) {
    if (row.toInt() != this->sensor->getBinning().row) {
      this->mainWindow->binRowBox->setPalette(this->pendingStyle);
    } else {
      this->mainWindow->binRowBox->setPalette(this->origStyle);
    }
  } else if (this->mainWindow->binningLabel->currentText().compare("Skip") ==
             0) {
    if (row.toInt() != this->sensor->getSkip().row) {
      this->mainWindow->binRowBox->setPalette(this->pendingStyle);
    } else {
      this->mainWindow->binRowBox->setPalette(this->origStyle);
    }
  }
}
void Pending::Bin_ColChanged(QString col) {
  if (this->mainWindow->binningLabel->currentText().compare("Binning") == 0) {
    if (col.toInt() != this->sensor->getBinning().collum) {
      this->mainWindow->binColBox->setPalette(this->pendingStyle);
    } else {
      this->mainWindow->binColBox->setPalette(this->origStyle);
    }
  } else if (this->mainWindow->binningLabel->currentText().compare("Skip") ==
             0) {
    if (col.toInt() != this->sensor->getSkip().collum) {
      this->mainWindow->binColBox->setPalette(this->pendingStyle);
    } else {
      this->mainWindow->binColBox->setPalette(this->origStyle);
    }
  }
}

void Pending::enablePending(bool en) {
  this->enabled = en;
  if (en) {
    connect(this->roiWindow->widthSpin, SIGNAL(valueChanged(int)), this,
            SLOT(ROI_widthChanged(int)));
    connect(this->roiWindow->heightSpin, SIGNAL(valueChanged(int)), this,
            SLOT(ROI_heightChanged(int)));
    connect(this->roiWindow->startXSpin, SIGNAL(valueChanged(int)), this,
            SLOT(ROI_startXChanged(int)));
    connect(this->roiWindow->startYSpin, SIGNAL(valueChanged(int)), this,
            SLOT(ROI_startYChanged(int)));

    connect(this->mainWindow->brightnessSpin, SIGNAL(valueChanged(int)), this,
            SLOT(BrightChanged(int)));
    connect(this->mainWindow->exposureSpin, SIGNAL(valueChanged(int)), this,
            SLOT(ExposureChanged(int)));
    connect(this->mainWindow->gainSpin, SIGNAL(valueChanged(double)), this,
            SLOT(GainChanged(double)));

    connect(this->mainWindow->AECCheck, SIGNAL(stateChanged(int)), this,
            SLOT(AECChanged(int)));
    connect(this->mainWindow->AGCCheck, SIGNAL(stateChanged(int)), this,
            SLOT(AGCChanged(int)));
    connect(this->mainWindow->HDRCheck, SIGNAL(stateChanged(int)), this,
            SLOT(HDRChanged(int)));
    connect(this->mainWindow->flipRowBox, SIGNAL(stateChanged(int)), this,
            SLOT(Flip_RowChanged(int)));
    connect(this->mainWindow->flipColBox, SIGNAL(stateChanged(int)), this,
            SLOT(Flip_ColChanged(int)));

    connect(this->mainWindow->binColBox, SIGNAL(currentTextChanged(QString)),
            this, SLOT(Bin_ColChanged(QString)));
    connect(this->mainWindow->binRowBox, SIGNAL(currentTextChanged(QString)),
            this, SLOT(Bin_RowChanged(QString)));
  } else {
    disconnect(this->roiWindow->widthSpin, SIGNAL(valueChanged(int)), this,
               SLOT(ROI_widthChanged(int)));
    disconnect(this->roiWindow->heightSpin, SIGNAL(valueChanged(int)), this,
               SLOT(ROI_heightChanged(int)));
    disconnect(this->roiWindow->startXSpin, SIGNAL(valueChanged(int)), this,
               SLOT(ROI_startXChanged(int)));
    disconnect(this->roiWindow->startYSpin, SIGNAL(valueChanged(int)), this,
               SLOT(ROI_startYChanged(int)));

    disconnect(this->mainWindow->brightnessSpin, SIGNAL(valueChanged(int)),
               this, SLOT(BrightChanged(int)));
    disconnect(this->mainWindow->exposureSpin, SIGNAL(valueChanged(int)), this,
               SLOT(ExposureChanged(int)));
    disconnect(this->mainWindow->gainSpin, SIGNAL(valueChanged(int)), this,
               SLOT(GainChanged(double)));

    disconnect(this->mainWindow->AECCheck, SIGNAL(stateChanged(int)), this,
               SLOT(AECChanged(int)));
    disconnect(this->mainWindow->AGCCheck, SIGNAL(stateChanged(int)), this,
               SLOT(AGCChanged(int)));
    disconnect(this->mainWindow->HDRCheck, SIGNAL(stateChanged(int)), this,
               SLOT(HDRChanged(int)));
    disconnect(this->mainWindow->flipRowBox, SIGNAL(stateChanged(int)), this,
               SLOT(Flip_RowChanged(int)));
    disconnect(this->mainWindow->flipColBox, SIGNAL(stateChanged(int)), this,
               SLOT(Flip_ColChanged(int)));

    disconnect(this->mainWindow->binColBox, SIGNAL(currentTextChanged(QString)),
               this, SLOT(Bin_ColChanged(QString)));
    disconnect(this->mainWindow->binRowBox, SIGNAL(currentTextChanged(QString)),
               this, SLOT(Bin_RowChanged(QString)));
  }
}

void Pending::setPending(QWidget *widget, bool pending) {
  if (pending) {
    widget->setPalette(this->pendingStyle);
  } else {
    widget->setPalette(this->origStyle);
  }
}
