// AYVolPan.cpp: файл реализации
//

#include "pch.h"
#include "BK.h"
#include "BKAYVolPan.h"
#include "afxdialogex.h"

// Диалоговое окно CAYVolPan

IMPLEMENT_DYNAMIC(CBKAYVolPan, CDialogEx)

CBKAYVolPan::CBKAYVolPan(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_AYVOLPAN_DLG, pParent)
	, m_nVolA(0)
	, m_nVolB(0)
	, m_nVolC(0)
	, m_nPanAL(0)
	, m_nPanBL(0)
	, m_nPanCL(0)
	, m_nPanAR(0)
	, m_nPanBR(0)
	, m_nPanCR(0)
{
	ZeroMemory(&m_orig, sizeof(CConfig::AYVolPan_s));
	ZeroMemory(&m_curr, sizeof(CConfig::AYVolPan_s));
}

CBKAYVolPan::~CBKAYVolPan()
= default;

void CBKAYVolPan::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_SLIDER_AYPANA, m_ctrPanA);
	DDX_Control(pDX, IDC_SLIDER_AYPANB, m_ctrPanB);
	DDX_Control(pDX, IDC_SLIDER_AYPANC, m_ctrPanC);
	DDX_Control(pDX, IDC_SLIDER_AYVOLA, m_ctrVolA);
	DDX_Control(pDX, IDC_SLIDER_AYVOLB, m_ctrVolB);
	DDX_Control(pDX, IDC_SLIDER_AYVOLC, m_ctrVolC);
	DDX_Text(pDX, IDC_STATIC_AYVOLA, m_nVolA);
	DDX_Text(pDX, IDC_STATIC_AYVOLB, m_nVolB);
	DDX_Text(pDX, IDC_STATIC_AYVOLC, m_nVolC);
	DDX_Text(pDX, IDC_STATIC_AYPANAL, m_nPanAL);
	DDX_Text(pDX, IDC_STATIC_AYPANBL, m_nPanBL);
	DDX_Text(pDX, IDC_STATIC_AYPANCL, m_nPanCL);
	DDX_Text(pDX, IDC_STATIC_AYPANAR, m_nPanAR);
	DDX_Text(pDX, IDC_STATIC_AYPANBR, m_nPanBR);
	DDX_Text(pDX, IDC_STATIC_AYPANCR, m_nPanCR);
}


BEGIN_MESSAGE_MAP(CBKAYVolPan, CDialogEx)
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
END_MESSAGE_MAP()


// Обработчики сообщений CAYVolPan


BOOL CBKAYVolPan::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	CConfig::AYVolPan_s s = g_Config.getVolPan();
	m_orig = s;
	m_curr = s;

	m_ctrPanA.SetRange(0, AY_VOLPAN_SCALE, FALSE);
	m_ctrPanA.SetPos(static_cast<int>(AY_VOLPAN_SCALE * (1.0 - s.A_P)));
	m_nPanAL = int(s.A_P * 100.0); m_nPanAR = 100 - m_nPanAL;
	m_ctrPanB.SetRange(0, AY_VOLPAN_SCALE, FALSE);
	m_ctrPanB.SetPos(static_cast<int>(AY_VOLPAN_SCALE * (1.0 - s.B_P)));
	m_nPanBL = int(s.B_P * 100.0); m_nPanBR = 100 - m_nPanBL;
	m_ctrPanC.SetRange(0, AY_VOLPAN_SCALE, FALSE);
	m_ctrPanC.SetPos(static_cast<int>(AY_VOLPAN_SCALE * (1.0 - s.C_P)));
	m_nPanCL = int(s.C_P * 100.0); m_nPanCR = 100 - m_nPanCL;

	m_ctrVolA.SetRange(0, AY_VOLPAN_SCALE, FALSE);
	m_ctrVolA.SetPos(static_cast<int>(AY_VOLPAN_SCALE * (1.0 - s.A_V)));
	m_nVolA = int(s.A_V * 100.0);
	m_ctrVolB.SetRange(0, AY_VOLPAN_SCALE, FALSE);
	m_ctrVolB.SetPos(static_cast<int>(AY_VOLPAN_SCALE * (1.0 - s.B_V)));
	m_nVolB = int(s.B_V * 100.0);
	m_ctrVolC.SetRange(0, AY_VOLPAN_SCALE, FALSE);
	m_ctrVolC.SetPos(static_cast<int>(AY_VOLPAN_SCALE * (1.0 - s.C_V)));
	m_nVolC = int(s.C_V * 100.0);

	int t = 0;
	while (t < AY_VOLPAN_SCALE)
	{
		m_ctrPanA.SetTic(t);
		m_ctrPanB.SetTic(t);
		m_ctrPanC.SetTic(t);
		m_ctrVolA.SetTic(t);
		m_ctrVolB.SetTic(t);
		m_ctrVolC.SetTic(t);
		t += 16;
	}

	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // Исключение: страница свойств OCX должна возвращать значение FALSE
}


void CBKAYVolPan::Save()
{
	g_Config.setVolPan(m_curr);
	UpdateData(FALSE);
}

void CBKAYVolPan::OnOK()
{
	Save();
	CDialogEx::OnOK();
}


void CBKAYVolPan::OnCancel()
{
	// при отмене, возвращаем всё как было
	g_Config.setVolPan(m_orig);

	CDialogEx::OnCancel();
}



void CBKAYVolPan::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
{
	auto pSlider = reinterpret_cast<CSliderCtrl *>(pScrollBar);

	if (pSlider == &m_ctrPanA)
	{
		m_curr.A_P = 1.0 - double(m_ctrPanA.GetPos()) / double(AY_VOLPAN_SCALE);
		m_nPanAL = int(m_curr.A_P * 100.0); m_nPanAR = 100 - m_nPanAL;
		Save();
	}
	else if (pSlider == &m_ctrPanB)
	{
		m_curr.B_P = 1.0 - double(m_ctrPanB.GetPos()) / double(AY_VOLPAN_SCALE);
		m_nPanBL = int(m_curr.B_P * 100.0); m_nPanBR = 100 - m_nPanBL;
		Save();
	}
	else if (pSlider == &m_ctrPanC)
	{
		m_curr.C_P = 1.0 - double(m_ctrPanC.GetPos()) / double(AY_VOLPAN_SCALE);
		m_nPanCL = int(m_curr.C_P * 100.0) ; m_nPanCR = 100 - m_nPanCL;
		Save();
	}

	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}


void CBKAYVolPan::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
{
	auto pSlider = reinterpret_cast<CSliderCtrl *>(pScrollBar);

	if (pSlider == &m_ctrVolA)
	{
		m_curr.A_V = 1.0 - double(m_ctrVolA.GetPos()) / double(AY_VOLPAN_SCALE);
		m_nVolA = int(m_curr.A_V * 100.0);
		Save();
	}
	else if (pSlider == &m_ctrVolB)
	{
		m_curr.B_V = 1.0 - double(m_ctrVolB.GetPos()) / double(AY_VOLPAN_SCALE);
		m_nVolB = int(m_curr.B_V * 100.0);
		Save();
	}
	else if (pSlider == &m_ctrVolC)
	{
		m_curr.C_V = 1.0 - double(m_ctrVolC.GetPos()) / double(AY_VOLPAN_SCALE);
		m_nVolC = int(m_curr.C_V * 100.0);
		Save();
	}

	CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
}
