// 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

// константы числа 100%
constexpr double VOL_VALUE_D = 100.0;

constexpr auto AY_VOLPAN_SLIDER_SCALE = 100; // масштаб слайдеров
constexpr auto AY_VOLPAN_SLIDER_TICK_STEP = 10; // шаг насечек слайдеров


BOOL CBKAYVolPan::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	CConfig::AYVolPan_s s = g_Config.getVolPan();
	m_orig = s;
	m_curr = s;
	m_nPanAL = s.nA_P;
	m_nPanAR = AY_PAN_BASE - s.nA_P;
	m_ctrPanA.SetRange(0, AY_VOLPAN_SLIDER_SCALE, FALSE);
	m_ctrPanA.SetPos(AY_VOLPAN_SLIDER_SCALE * m_nPanAR / AY_PAN_BASE);
	m_nPanBL = s.nB_P;
	m_nPanBR = AY_PAN_BASE - s.nB_P;
	m_ctrPanB.SetRange(0, AY_VOLPAN_SLIDER_SCALE, FALSE);
	m_ctrPanB.SetPos(AY_VOLPAN_SLIDER_SCALE * m_nPanBR / AY_PAN_BASE);
	m_nPanCL = s.nC_P;
	m_nPanCR = AY_PAN_BASE - s.nC_P;
	m_ctrPanC.SetRange(0, AY_VOLPAN_SLIDER_SCALE, FALSE);
	m_ctrPanC.SetPos(AY_VOLPAN_SLIDER_SCALE * m_nPanCR / AY_PAN_BASE);
	m_ctrVolA.SetRange(0, AY_VOLPAN_SLIDER_SCALE, FALSE);
	m_ctrVolA.SetPos(static_cast<int>(AY_VOLPAN_SLIDER_SCALE * (AY_VOL_BASE - s.A_V)));
	m_nVolA = int(s.A_V * VOL_VALUE_D);
	m_ctrVolB.SetRange(0, AY_VOLPAN_SLIDER_SCALE, FALSE);
	m_ctrVolB.SetPos(static_cast<int>(AY_VOLPAN_SLIDER_SCALE * (AY_VOL_BASE - s.B_V)));
	m_nVolB = int(s.B_V * VOL_VALUE_D);
	m_ctrVolC.SetRange(0, AY_VOLPAN_SLIDER_SCALE, FALSE);
	m_ctrVolC.SetPos(static_cast<int>(AY_VOLPAN_SLIDER_SCALE * (AY_VOL_BASE - s.C_V)));
	m_nVolC = int(s.C_V * VOL_VALUE_D);

	for (int t = 0; t < AY_VOLPAN_SLIDER_SCALE; t += AY_VOLPAN_SLIDER_TICK_STEP)
	{
		m_ctrPanA.SetTic(t);
		m_ctrPanB.SetTic(t);
		m_ctrPanC.SetTic(t);
		m_ctrVolA.SetTic(t);
		m_ctrVolB.SetTic(t);
		m_ctrVolC.SetTic(t);
	}

	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_nPanAR = AY_PAN_BASE * m_ctrPanA.GetPos() / AY_VOLPAN_SLIDER_SCALE;
		m_curr.nA_P = m_nPanAL = AY_PAN_BASE - m_nPanAR;
		Save();
	}
	else if (pSlider == &m_ctrPanB)
	{
		m_nPanBR = AY_PAN_BASE * m_ctrPanB.GetPos() / AY_VOLPAN_SLIDER_SCALE;
		m_curr.nB_P = m_nPanBL = AY_PAN_BASE - m_nPanBR;
		Save();
	}
	else if (pSlider == &m_ctrPanC)
	{
		m_nPanCR = AY_PAN_BASE * m_ctrPanC.GetPos() / AY_VOLPAN_SLIDER_SCALE;
		m_curr.nC_P = m_nPanCL = AY_PAN_BASE - m_nPanCR;
		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 = AY_VOL_BASE - double(m_ctrVolA.GetPos()) / double(AY_VOLPAN_SLIDER_SCALE);
		m_nVolA = int(m_curr.A_V * VOL_VALUE_D);
		Save();
	}
	else if (pSlider == &m_ctrVolB)
	{
		m_curr.B_V = AY_VOL_BASE - double(m_ctrVolB.GetPos()) / double(AY_VOLPAN_SLIDER_SCALE);
		m_nVolB = int(m_curr.B_V * VOL_VALUE_D);
		Save();
	}
	else if (pSlider == &m_ctrVolC)
	{
		m_curr.C_V = AY_VOL_BASE - double(m_ctrVolC.GetPos()) / double(AY_VOLPAN_SLIDER_SCALE);
		m_nVolC = int(m_curr.C_V * VOL_VALUE_D);
		Save();
	}

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