/****************************************************************************************
fmhd main process
******************************************************************************************/
RETURN_CODE fmhd_amhd_work_mode(void)
{
	RETURN_CODE ret = SUCCESS;

	//u8 temp_str[16];
#ifndef OPTION__OPERATE_AS_SLAVE_NO_MMI
	static bit hd_flag = 0;
	static u8 display_item_state = 0;
#endif

	if( power_flag ==0)
	{
		power_flag =1;
		fmhd_amhd_initialize();
	}

#ifndef OPTION__OPERATE_AS_SLAVE_NO_MMI
	ret |=fmhd_amhd_key_process();
	
	if(display_item_str_4ms >0)
	{
		//display_refresh_flag = 1;
		return 0;// wait for time up
	}

    if(ber_running_flag == 1)
    {
        fmhd_BER_update_display();
        return 0; // We do not want to leave this screen until we have stopped running the BER test
    }

	 if(display_refresh_flag==1)
	{
		display_refresh_flag=0;
		ret |= display_hd_ui();
	}
	else //if(display_item_str_4ms == 0)
	{
	
		display_item_str_4ms = STATUS_UPDATE_INTERVAL;
		if(item_loop == ITEM_AUTO_ROLL) // display will auto change next next 
		{
			if(work_mode == fmhd)
			{
				if(display_item_state > sizeof(FMHD_DISPLAY_ITEM)/sizeof(FMHD_DISPLAY_ITEM[0])-1)
				{
					display_item_state = 1;
				}
			}
        #ifdef OPTION__INCLUDE_MODE__AMHD
			else if(work_mode == amhd)
			{
				if(display_item_state > sizeof(AMHD_DISPLAY_ITEM)/sizeof(AMHD_DISPLAY_ITEM[0])-1)
				{
					display_item_state = 1;
				}
			}
        #endif // OPTION__INCLUDE_MODE__AMHD		
		}
		else
		{
			display_item_state = item_loop;// fix display special item switched by hold tune knobs
		}

		if(work_mode == fmhd)
		{
			if (display_data_item(FMHD_DISPLAY_ITEM[display_item_state]) == ITEM_NEXT)
			{
				display_item_state ++;
			}   
			ret = UpdateMetrics();
			if(MetricsGetFMHDPtr()->AUDIO_SOURCE != hd_flag)
			{
				hd_flag= MetricsGetFMHDPtr()->AUDIO_SOURCE;
				show_freq(0,0);
			}			 
		}
    #ifdef OPTION__INCLUDE_MODE__AMHD
		else
		{
			if (display_data_item(AMHD_DISPLAY_ITEM[display_item_state]) == ITEM_NEXT)
			{
				display_item_state ++;
			}  
			ret = UpdateMetrics();
			if(MetricsGetAMHDPtr()->AUDIO_SOURCE != hd_flag)
			{
				hd_flag= MetricsGetAMHDPtr()->AUDIO_SOURCE;
				show_freq(0,0);
			}			  
		}
    #endif //OPTION__INCLUDE_MODE__AMHD		
	}
#endif //OPTION__OPERATE_AS_SLAVE_NO_MMI

#ifdef OPTION__OPERATE_AS_SLAVE_NO_MMI
	ret |= UpdateMetrics();
	ret |= UpdateDataServiceData();
#endif //OPTION__OPERATE_AS_SLAVE_NO_MMI

    ret |= UpdateServiceList(); // Need to periodically check for updates to the service list - reconfiguration
	return ret;
}
CasperAppWindow::CasperAppWindow(QWidget *parent)
    : QMainWindow(parent), ctx(new PrivateAppContext) {
  ctx->mWindowPtr->setupUi(this);
  setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint);
  ctx->mWindowPtr->toolBar->setFloatable(false);
  ctx->mWindowPtr->toolBar->setMovable(false);
  ctx->mWindowPtr->toolBar->setContextMenuPolicy(Qt::NoContextMenu);
  ctx->mWindowPtr->toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
  ctx->mWindowPtr->toolBar->addAction(
      QIcon(":/casper/artwork/casper_ui_toolbar_settings.svg"), "Settings");

#ifdef Q_OS_WIN
  setMinimumSize(211, 250);
  setMaximumSize(211, 250);
  setWindowTitle("Casper DNS");
  setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
#endif
  ctx->mServiceListPtr = Casper::ServiceList::Get();
  ctx->mServiceListPtr->Init();

  connect(ctx->mServiceListPtr, &Casper::ServiceList::service_added, this,
          [this]() {});

  UpdateServiceList();

  ctx->mWindowPtr->m_service_list->setCurrentText(
      Casper::ServiceList::Get()->GetActiveService().getName());

  connect(ctx->mWindowPtr->m_service_btn, &UIButton::toggled, this,
          [this](bool checked) {
    if (checked) {
      UpdateSystemDNS();
    } else {
      ctx->mWindowPtr->m_service_btn->setButtonActionState(
          UIButton::kButtonActionInactive);

      Casper::Client::Get()->RollbackDNS();
      // ctx->mWindowPtr->dns_status->setText("DNS DISABLED");
      // statusBar()->showMessage(tr("OFF"));
    }
  });

  connect(ctx->mWindowPtr->m_service_list, SIGNAL(activated(QString)), this,
          SLOT(on_activated(QString)));
  connect(ctx->mWindowPtr->m_service_list, SIGNAL(highlighted(QString)), this,
          SLOT(on_highligted(QString)));

  connect(Casper::ServiceList::Get(), &Casper::ServiceList::service_added, this,
          [this](const QString &name) { UpdateServiceList(); });

  connect(Casper::ServiceList::Get(), &Casper::ServiceList::service_removed,
          this, [this](const QString &name) {
    for (int i = 0;
         i < Casper::ServiceList::Get()->GetCurrentServiceList().count(); i++) {
      if (ctx->mWindowPtr->m_service_list->itemText(i) == name) {
        ctx->mWindowPtr->m_service_list->removeItem(i);
        break;
      }
    }
  });

  connect(ctx->mWindowPtr->toolBar, &QToolBar::actionTriggered, this,
          [this](QAction *action) {
    if (!action)
      return;
    ConfigureDialog *dialog = new ConfigureDialog(this);
    dialog->show();
  });
}