AnalysisView::AnalysisView(RecordingManager &mngr, Widgets::Widget *parent) : Widgets::Widget(parent), _manager(mngr) {
	_audioView = new AnalysisAudioView(mngr, _spikes, this);
	_audioView->setSizePolicy(Widgets::SizePolicy(Widgets::SizePolicy::Expanding, Widgets::SizePolicy::Expanding));
	_audioView->addChannel(0);


	Widgets::PushButton *closeButton = new Widgets::PushButton(this);
	closeButton->clicked.connect(this, &AnalysisView::closePressed);
	closeButton->setNormalTex(Widgets::TextureGL::get("data/analysis.png"));
	closeButton->setHoverTex(Widgets::TextureGL::get("data/analysishigh.png"));

	Widgets::Label *label = new Widgets::Label(this);
	label->setText("Spike Analysis");
	label->updateSize();

	Widgets::ScrollBar *seekBar = new Widgets::ScrollBar(Widgets::Horizontal, this);
	seekBar->setRange(0,1000);
	seekBar->setPageStep(25);
	seekBar->valueChanged.connect((AudioView *)_audioView, &AudioView::setRelOffset);
 	_audioView->relOffsetChanged.connect(seekBar, &Widgets::ScrollBar::updateValue);
	seekBar->setValue(1000);

	Widgets::PushButton *saveButton = new Widgets::PushButton(this);
	saveButton->setNormalTex(Widgets::TextureGL::get("data/save.png"));
	saveButton->setHoverTex(Widgets::TextureGL::get("data/savehigh.png"));
	saveButton->setSizeHint(Widgets::Size(64,64));
	saveButton->clicked.connect(this, &AnalysisView::savePressed);
	Widgets::BoxLayout *saveBox = new Widgets::BoxLayout(Widgets::Horizontal);

	saveBox->addWidget(saveButton);
	saveBox->setAlignment(Widgets::AlignCenter);
	Widgets::BoxLayout *vbox = new Widgets::BoxLayout(Widgets::Vertical, this);
	Widgets::BoxLayout *hbox = new Widgets::BoxLayout(Widgets::Horizontal);
	hbox->addSpacing(10);
	hbox->addWidget(closeButton);
	hbox->addSpacing(17);
	hbox->addWidget(label, Widgets::AlignVCenter);
	vbox->addSpacing(10);
	vbox->addLayout(hbox);
	vbox->addSpacing(20);
	vbox->addWidget(_audioView, Widgets::AlignCenter);
	vbox->addWidget(seekBar);
	vbox->addSpacing(10);
	vbox->addLayout(saveBox);
	vbox->addSpacing(20);

	vbox->update();

	_spikes.findSpikes(_manager.fileName(), _manager.selectedVDevice(), _manager.sampleRate()/1000 /* 1 ms */);

	_wasThreshMode = _manager.threshMode();
	_manager.setThreshMode(false);
	_manager.setPos(_manager.fileLength()/2);
}
ThresholdPanel::ThresholdPanel(RecordingManager &manager, AnalysisManager &anaman, Widgets::Widget *parent) : Widgets::Widget(parent) {

	_manager = &manager;
	_triggerButton = new Widgets::PushButton(this);
	setTriggerButtonImage();
	_triggerButton->setSizeHint(Widgets::Size(42,32));
	_triggerButton->setRightPadding(10);
	_triggerButton->clicked.connect(this, &ThresholdPanel::triggerPressed);

	_ekgButton = new Widgets::PushButton(this);
	_ekgButton->setNormalTex(Widgets::TextureGL::get("data/ekg.bmp"));
	_ekgButton->setHoverTex(Widgets::TextureGL::get("data/ekghigh.bmp"));
	_ekgButton->setSizeHint(Widgets::Size(42,32));
	_ekgButton->setRightPadding(10);
	_ekgButton->clicked.connect(this, &ThresholdPanel::ekgPressed);
	if(!(_manager->serialMode() || _manager->hidMode()))
	{
		_ekgButton->setVisible(false);
	}


	_ekgWidget = new EkgWidget(anaman, this);
	manager.triggered.connect(_ekgWidget,&EkgWidget::beat);
	_speakerButton = new Widgets::PushButton(this);
	_speakerButton->setNormalTex(Widgets::TextureGL::get("data/speakeroff.bmp"));
	_speakerButton->setSizeHint(Widgets::Size(20,20));
	_speakerButton->clicked.connect(this, &ThresholdPanel::speakerPressed);

	_avg = new Widgets::ScrollBar(Widgets::Horizontal,this);
	_avg->setRange(1,50);
	_avg->setPageStep(5);
	_avg->valueChanged.connect(&manager, &RecordingManager::setThreshAvgCount);
	_avg->setSizeHint(Widgets::Size(250,20));
	_avg->setSizePolicy(Widgets::SizePolicy(Widgets::SizePolicy::Fixed, Widgets::SizePolicy::Maximum));

	Widgets::Label *label = new Widgets::Label(this);
	label->setText("00");
	label->updateSize();

	_avg->valueChanged.connect(label, &Widgets::Label::setText);
	_avg->setValue(1);

	_switchLayout = new Widgets::SwitchLayout();

	Widgets::BoxLayout *avgBar = new Widgets::BoxLayout(Widgets::Horizontal);
	avgBar->addWidget(_avg, Widgets::AlignVCenter);
	avgBar->addSpacing(10);
	avgBar->addWidget(label, Widgets::AlignVCenter);

	Widgets::BoxLayout *ekgBar = new Widgets::BoxLayout(Widgets::Horizontal);
	ekgBar->addWidget(_ekgWidget, Widgets::AlignVCenter);
	ekgBar->addWidget(_speakerButton, Widgets::AlignVCenter);

	_switchLayout->addLayout(avgBar);
	_switchLayout->addLayout(ekgBar);


	Widgets::BoxLayout *layout = new Widgets::BoxLayout(Widgets::Horizontal, this);
	layout->addWidget(_ekgButton);
	layout->addWidget(_triggerButton);


	// layout->addWidget(_thresholdWidget, Widgets::AlignTop);
	//layout->addSpacing(10);

	//layout->addSpacing(10);
	layout->addLayout(_switchLayout);
	layout->update();

}
ConfigView::ConfigView(RecordingManager &mngr, AudioView &audioView, Widget *parent) : Widget(parent), _manager(mngr), _audioView(audioView) {
	Widgets::PushButton *closeButton = new Widgets::PushButton(this);
	closeButton->clicked.connect(this, &ConfigView::closePressed);
	closeButton->setNormalTex(Widgets::TextureGL::get("data/config.png"));
	closeButton->setHoverTex(Widgets::TextureGL::get("data/confighigh.png"));

	Widgets::Label *topLabel = new Widgets::Label(this);
	topLabel->setText("Config");
	topLabel->updateSize();

	std::vector<Widgets::Color> c(AudioView::COLORS, AudioView::COLORS+AudioView::COLOR_NUM);
	_clrs.resize(_manager.recordingDevices().size());
	_catchers.reserve(_clrs.size());

	Widgets::Widget *group = new Widgets::Widget(this);
	group->setSizeHint(Widgets::Size(500,400));
	Widgets::BoxLayout *gvbox = new Widgets::BoxLayout(Widgets::Vertical, group);

	if(!_manager.fileMode()) {
		Widgets::BoxLayout *mutehbox = new Widgets::BoxLayout(Widgets::Horizontal);
		Widgets::Label *muteLabel = new Widgets::Label(group);
		muteLabel->setText("Mute Speakers while recording:");
		muteLabel->updateSize();

		_muteCKBox = new Widgets::PushButton(group);
		if(_manager.player().volume() == 0)
			_muteCKBox->setNormalTex(Widgets::TextureGL::get("data/ckboxon.png"));
		else
			_muteCKBox->setNormalTex(Widgets::TextureGL::get("data/ckboxoff.png"));
		_muteCKBox->setSizeHint(Widgets::Size(16,16));
		_muteCKBox->clicked.connect(this, &ConfigView::mutePressed);

		mutehbox->addWidget(muteLabel);
		mutehbox->addSpacing(10);
		mutehbox->addWidget(_muteCKBox, Widgets::AlignVCenter);
		mutehbox->addSpacing(50);
		gvbox->addLayout(mutehbox);
		gvbox->addSpacing(40);
	}

	for(unsigned int i = 0; i < _manager.recordingDevices().size(); i++) {
		if(!_manager.recordingDevices()[i].enabled)
			continue;
		_clrs[i] = new ColorDropDownList(group);
		_clrs[i]->setContent(c);

		_catchers.push_back(SignalCatcher(i, this));
		_clrs[i]->selectionChanged.connect(&_catchers[i], &SignalCatcher::catchColor);
		Widgets::Label *name = new Widgets::Label(group);
		name->setText(_manager.recordingDevices()[i].name.c_str());
		name->updateSize();

		Widgets::BoxLayout *ghbox = new Widgets::BoxLayout(Widgets::Horizontal);
		ghbox->addWidget(_clrs[i]);
		ghbox->addSpacing(20);
		ghbox->addWidget(name,Widgets::AlignVCenter);
		gvbox->addLayout(ghbox);
		gvbox->addSpacing(15);
	}

	for(int i = 0; i < audioView.channelCount(); i++)
		_clrs[audioView.channelVirtualDevice(i)]->setSelection(audioView.channelColor(i));



    // -------- Serial configuration
    
    if(!_manager.fileMode())
    {
        //Serial  config widgets
        Widgets::Label *name2 = new Widgets::Label(group);
        name2->setText("Select port:");
        name2->updateSize();
        gvbox->addSpacing(0);
        gvbox->addWidget(name2, Widgets::AlignLeft);
        
        
        
        //Dropdown for select port
        Widgets::BoxLayout *serialHbox = new Widgets::BoxLayout(Widgets::Horizontal);
        serialPortWidget = new DropDownList(group);
        serialPortWidget->clear();
        std::list<std::string> sps =  _manager.serailPortsList();
        std::list<std::string>::iterator it;
        for(it = sps.begin();it!=sps.end();it++)
        {
            serialPortWidget->addItem(it->c_str());
        }
        serialPortWidget->setSelection(_manager.serialPortIndex());
        _catchers.push_back(SignalCatcher(_catchers.size(), this));
        serialPortWidget->indexChanged.connect(&_catchers[_catchers.size()-1], &SignalCatcher::catchPort);
        serialPortWidget->setDisabled(_manager.serialMode());
        
        serialHbox->addWidget(serialPortWidget);
        serialHbox->addSpacing(5);

    

    
        //Button for connect to serial
        _connectButton = new Widgets::PushButton(group);
        _connectButton->clicked.connect(this, &ConfigView::connectPressed);
        if(_manager.serialMode())
        {
            _connectButton->setNormalTex(Widgets::TextureGL::get("data/connected.png"));
            _connectButton->setHoverTex(Widgets::TextureGL::get("data/connected.png"));
        }
        else
        {
            _connectButton->setNormalTex(Widgets::TextureGL::get("data/disconnected.png"));
            _connectButton->setHoverTex(Widgets::TextureGL::get("data/disconnected.png"));
        }
        _connectButton->setSizeHint(Widgets::Size(26,26));
        serialHbox->addWidget(_connectButton);
        serialHbox->update();
        gvbox->addSpacing(3);
        gvbox->addLayout(serialHbox);
        
        
        if(_manager.serialMode())
        {
                //Number of channels chooser
                Widgets::BoxLayout *numberOfChannelsHbox = new Widgets::BoxLayout(Widgets::Horizontal);
                
                Widgets::Label *numChannelsLabel = new Widgets::Label(group);
                numChannelsLabel->setText("Number of channels:");
                numChannelsLabel->updateSize();
                numberOfChannelsHbox->addWidget(numChannelsLabel);
                numberOfChannelsHbox->addSpacing(5);
                
                
                
                numberOfChannelsWidget = new DropDownList(group, 50,30);
                numberOfChannelsWidget->clear();

                numberOfChannelsWidget->addItem("1");
                numberOfChannelsWidget->addItem("2");
                numberOfChannelsWidget->addItem("3");
                numberOfChannelsWidget->addItem("4");
                numberOfChannelsWidget->addItem("5");
                numberOfChannelsWidget->addItem("6");
                
                numberOfChannelsWidget->setSelection(_manager.numberOfSerialChannels()-1);
                _catchers.push_back(SignalCatcher(_catchers.size(), this));
                numberOfChannelsWidget->indexChanged.connect(&_catchers[_catchers.size()-1], &SignalCatcher::setNumOfChannelsHandler);
                numberOfChannelsWidget->setDisabled(!_manager.serialMode());
                
                numberOfChannelsHbox->addWidget(numberOfChannelsWidget);

                numberOfChannelsHbox->update();
                
                gvbox->addSpacing(10);
                gvbox->addLayout(numberOfChannelsHbox);
        }
        
    
    }
    


	gvbox->update();

	Widgets::BoxLayout *vbox = new Widgets::BoxLayout(Widgets::Vertical, this);
	Widgets::BoxLayout *hbox = new Widgets::BoxLayout(Widgets::Horizontal);
	hbox->addSpacing(10);
	hbox->addWidget(closeButton);
	hbox->addSpacing(17);
	hbox->addWidget(topLabel, Widgets::AlignVCenter);
	vbox->addSpacing(10);
	vbox->addLayout(hbox);
	vbox->addSpacing(20);
	vbox->addWidget(group, Widgets::AlignCenter);

	vbox->update();

}