using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections;
using DPSDK_RES = System.Int32;

namespace DPSDK_CSharpDemo
{
    public partial class CSharpDemo : Form
    {
        private void ShowVtCallInfo(string strInfo)
        {
            string strShow = textVtCallHandleResult.Text + "\r\n" + strInfo;
            textVtCallHandleResult.Text = strShow;
        }
        private void OnSCSStatusEventNotify(ref DPSDK_SCS_STATUS_NOTIFY pNotify)
        {
            textVtCallHandleResult.Text = AppData._CS("SCSStatusEventNotify iStatus = %1", pNotify.iStatus.ToString());
        }

        private void OnVtCallInviteEventNotify(ref DPSDK_VTCALL_INVITE_NOTIFY pNotify)
        {
            m_struVtCallInviteNotify.szUserId = pNotify.szUserId;
            m_struVtCallInviteNotify.szRtpServIP = pNotify.szRtpServIP;
            m_struVtCallInviteNotify.iCallType = pNotify.iCallType;
            m_struVtCallInviteNotify.iAudioType = pNotify.iAudioType;

            m_struVtCallInviteNotify.iTalkBit = pNotify.iTalkBit;
            m_struVtCallInviteNotify.iSampleRate = pNotify.iSampleRate;
            m_struVtCallInviteNotify.iRtpAPort = pNotify.iRtpAPort;
            m_struVtCallInviteNotify.iRtpVPort = pNotify.iRtpVPort;

            m_struVtCallInviteNotify.iCallId = pNotify.iCallId;
            m_struVtCallInviteNotify.iDlgId = pNotify.iDlgId;
            m_struVtCallInviteNotify.iTId = pNotify.iTId;

            m_bVtCallInvite = true;
            string strShowInfo = AppData._CS("InviteVtCallEventNotify.");
            strShowInfo += "\r\n";
            strShowInfo += AppData._CS("szUserId=%1 szRtpServIP=%2 iCallId=%3 iCallType=%4 iDlgId=%5 ", pNotify.szUserId, pNotify.szRtpServIP, pNotify.iCallId.ToString(), pNotify.iCallType.ToString(), pNotify.iDlgId.ToString());
            strShowInfo += AppData._CS("iAudioType=%1 iSampleRate=%2 iTalkBit=%3 iRtpAPort=%4 iRtpVPort=%5.", pNotify.iAudioType.ToString(), pNotify.iSampleRate.ToString(), pNotify.iTalkBit.ToString(), pNotify.iRtpAPort.ToString(), pNotify.iRtpVPort.ToString());
            ShowVtCallInfo(strShowInfo);
        }

        private void OnVtCallByeEventNotify(ref DPSDK_VTCALL_BYE_NOTIFY pNotify)
        {
            textVtCallHandleResult.Text = AppData._CS("VtCallByeEventNotify szCallerId = %1", pNotify.szCallerId);
        }

        private void OnVtCallRingEventNotify(ref DPSDK_VTCALL_RING_NOTIFY pNotify)
        {
            string qstrShowInfo;
            qstrShowInfo = AppData._CS("VtCallRingEventNotify szUserId = %1 iCallId = %2 iDlgId = %3 iTId = %4", pNotify.szUserId, pNotify.iCallId.ToString(), pNotify.iDlgId.ToString(), pNotify.iTId.ToString());
            textVtCallHandleResult.Text = qstrShowInfo;
            IntPtr iCallStatus = (IntPtr)DPSDK_CALL_STATUS.CALL_STATUS_CALLING;
            DPSDK_RES iRet = DPSDK_ModifyVtCallStatus(m_iSessionID, m_iVtCallMediaSessionID, iCallStatus);
        }

        private void OnVtCallCancelEventNotify(ref DPSDK_VTCALL_CANCEL_NOTIFY pNotify)
        {
            textVtCallHandleResult.Text = AppData._CS("VtCallCancelEventNotify szUserId = %1 iDlgId = %2", pNotify.szUserId, pNotify.iDlgId.ToString());
        }

        private void OnVtCallBusyEventNotify(ref DPSDK_VTCALL_BUSY_NOTIFY pNotify)
        {
            textVtCallHandleResult.Text = AppData._CS("VtCallBusyEventNotify szUserId = %1 iDlgId = %2", pNotify.szUserId, pNotify.iDlgId.ToString());
        }

        private void OnSCSMessageEventNotify(ref DPSDK_SCS_MESSAGE_NOTIFY pNotify)
        {
            string strJson = Marshal.PtrToStringAnsi(pNotify.pJsonData);
	        textVtCallHandleResult.Text = AppData._CS("SCSMessageEventNotify JsonData = %1", strJson);
        }

        private void btnVtCallInviteVtCall_Click(object sender, EventArgs e)
        {
            if (m_iVtCallMediaSessionID.ToInt32() > 0)
            {
                DPSDK_ByeVtCall(m_iSessionID, m_iVtCallMediaSessionID);
                DPSDK_RES iRet = DPSDK_StopVtCall(m_iSessionID, m_iVtCallMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    textVtCallHandleResult.Text = AppData._CS("stop vtcall success");
                }
                else
                {
                    textVtCallHandleResult.Text = AppData._CS("stop vtcall failed, iRet = %1", iRet.ToString());
                }
                DPSDK_StopRealPlay(m_iSessionID, m_iVtCallRealMediaSessionID);
		        ClearVideoParam();
            }
            else
            {
                if (m_bVtCallInvite == true)
                {
                    DPSDK_REALPLAY_PARAM pRealPlayParam = new DPSDK_REALPLAY_PARAM();
                    pRealPlayParam.struMediaBaseParam.pHWnd = pictureBoxVtCallVideo.Handle;
                    pRealPlayParam.struMediaBaseParam.szCodeId = textVtCallDeviceID.Text;
                    pRealPlayParam.struMediaBaseParam.iStreamType = textVtCallStreamType.Text != "" ? Convert.ToInt32(textVtCallStreamType.Text) : 0;
                    pRealPlayParam.struMediaBaseParam.iDataType = textVtCallDataType.Text != "" ? Convert.ToInt32(textVtCallDataType.Text) : 0;
                    pRealPlayParam.struMediaBaseParam.iDecodeType = (int)(checkBoxVtCallIsDecode.CheckState == CheckState.Checked ? DPSDK_DECODE_TYPE.DPSDK_DECODE_HW : DPSDK_DECODE_TYPE.DPSDK_DECODE_SW);
                    pRealPlayParam.struMediaBaseParam.iStreamMode = textVtCallStreamMode.Text != "" ? Convert.ToInt32(textVtCallStreamMode.Text) : 0;
                    pRealPlayParam.struMediaBaseParam.uiDelayTime = textVtCallDelayTime.Text != "" ? Convert.ToUInt32(textVtCallDelayTime.Text) : 0;
                    DPSDK_RES iRet = DPSDK_StartRealPlay(m_iSessionID, ref pRealPlayParam, ref m_iVtCallRealMediaSessionID);
                    if (iRet == DPSDK_SUCCESS)
                    {
                        textVtCallHandleResult.Text = AppData._CS("start video success");
                    }
                    else
                    {
                        textVtCallHandleResult.Text = AppData._CS("start video failed, iRet = %1", iRet.ToString());
                    }

                    DPSDK_START_VTCALL_PARAM struStartVtCallParam = new DPSDK_START_VTCALL_PARAM();
                    struStartVtCallParam.szUserId = textVtCallPhoneNum.Text;
                    struStartVtCallParam.iCallType = textVtCallCallType.Text != "" ? Convert.ToInt32(textVtCallCallType.Text) : 0;
                    struStartVtCallParam.iAudioType = textVtCallAudioType.Text != "" ? Convert.ToInt32(textVtCallAudioType.Text) : 0;
                    IntPtr pStartVtCallRes = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DPSDK_START_VTCALL_RES)));
                    iRet = DPSDK_StartVtCall(m_iSessionID, ref struStartVtCallParam, ref m_iVtCallMediaSessionID, pStartVtCallRes);
                    if (iRet == DPSDK_SUCCESS)
                    {
                        textVtCallHandleResult.Text = AppData._CS("start vtcall success");
                        this.btnVtCallStartVtCall.Text = AppData._CS("StopVtCall");
                    }
                    else
                    {
                        textVtCallHandleResult.Text = AppData._CS("start vtcall failed, iRet = %1", iRet.ToString());
                    }
                    Marshal.FreeHGlobal(pStartVtCallRes);
                }
            }
        }

        private void btnVtCallStartVtCall_Click(object sender, EventArgs e)
        {
            if (m_iVtCallMediaSessionID.ToInt32() > 0)
            {
                DPSDK_RES iRet = DPSDK_StopVtCall(m_iSessionID, m_iVtCallMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    textVtCallHandleResult.Text = AppData._CS("stop vtcall success");
                }
                else
                {
                    textVtCallHandleResult.Text = AppData._CS("stop vtcall failed, iRet = %1", iRet.ToString());
                }
                DPSDK_StopRealPlay(m_iSessionID, m_iVtCallRealMediaSessionID);
                ClearVideoParam();
            }
            else
            {
                DPSDK_REALPLAY_PARAM pRealPlayParam = new DPSDK_REALPLAY_PARAM();
                pRealPlayParam.struMediaBaseParam.pHWnd = pictureBoxVtCallVideo.Handle;
                pRealPlayParam.struMediaBaseParam.szCodeId = textVtCallDeviceID.Text;
                pRealPlayParam.struMediaBaseParam.iStreamType = textVtCallStreamType.Text != "" ? Convert.ToInt32(textVtCallStreamType.Text) : 0;
                pRealPlayParam.struMediaBaseParam.iDataType = textVtCallDataType.Text != "" ? Convert.ToInt32(textVtCallDataType.Text) : 0;
                pRealPlayParam.struMediaBaseParam.iDecodeType = (int)(checkBoxVtCallIsDecode.CheckState == CheckState.Checked ? DPSDK_DECODE_TYPE.DPSDK_DECODE_HW : DPSDK_DECODE_TYPE.DPSDK_DECODE_SW);
                pRealPlayParam.struMediaBaseParam.iStreamMode = textVtCallStreamMode.Text != "" ? Convert.ToInt32(textVtCallStreamMode.Text) : 0;
                pRealPlayParam.struMediaBaseParam.uiDelayTime = textVtCallDelayTime.Text != "" ? Convert.ToUInt32(textVtCallDelayTime.Text) : 0;
                DPSDK_RES iRet = DPSDK_StartRealPlay(m_iSessionID, ref pRealPlayParam, ref m_iVtCallRealMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    textVtCallHandleResult.Text = AppData._CS("start video success");
                }
                else
                {
                    textVtCallHandleResult.Text = AppData._CS("start video failed, iRet = %1", iRet.ToString());
                }

                DPSDK_INVITE_VTCALL_PARAM struInviteVtCallParam = new DPSDK_INVITE_VTCALL_PARAM();
                struInviteVtCallParam.szUserId = m_struVtCallInviteNotify.szUserId;
                struInviteVtCallParam.szRtpServIP = m_struVtCallInviteNotify.szRtpServIP;
                struInviteVtCallParam.iCallType = m_struVtCallInviteNotify.iCallType;
                struInviteVtCallParam.iAudioType = m_struVtCallInviteNotify.iAudioType;

                struInviteVtCallParam.iAudioBit = m_struVtCallInviteNotify.iTalkBit;
                struInviteVtCallParam.iSampleRate = m_struVtCallInviteNotify.iSampleRate;
                struInviteVtCallParam.iRtpAPort = m_struVtCallInviteNotify.iRtpAPort;
                struInviteVtCallParam.iRtpVPort = m_struVtCallInviteNotify.iRtpVPort;

                struInviteVtCallParam.iCallId = m_struVtCallInviteNotify.iCallId;
                struInviteVtCallParam.iDlgId = m_struVtCallInviteNotify.iDlgId;
                struInviteVtCallParam.iTid = m_struVtCallInviteNotify.iTId;
                iRet = DPSDK_InviteVtCall(m_iSessionID, ref struInviteVtCallParam, ref m_iVtCallMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    textVtCallHandleResult.Text = AppData._CS("invite vtcall success");
                }
                else
                {
                    textVtCallHandleResult.Text = AppData._CS("invite vtcall failed, iRet = %1", iRet.ToString());
                }
            }
        }

        private void btnCancelVtCall_Click(object sender, EventArgs e)
        {
            //Visual intercom has been initiated, and the intercom can be cancelled when the other third **** does not answer
	        if (m_iVtCallMediaSessionID.ToInt32() > 0)
	        {
	            DPSDK_VTCALL_CANCEL_PARAM pParam = new DPSDK_VTCALL_CANCEL_PARAM();
                pParam.szUserId = m_struVtCallInviteNotify.szUserId;
                pParam.iCallId = m_struVtCallInviteNotify.iCallId;
                pParam.iDlgId = m_struVtCallInviteNotify.iDlgId;

	            DPSDK_RES iRet = DPSDK_CancelVtCall(m_iSessionID, ref pParam);
	            if (iRet == DPSDK_SUCCESS)
	            {
                    this.btnVtCallStartVtCall.Text = AppData._CS("CancelVtCall success.");
	            }
	            else
	            {
                    this.btnVtCallStartVtCall.Text = AppData._CS("CancelVtCall failed, iRet = %1", iRet.ToString());
	            }
                if (m_iVtCallRealMediaSessionID.ToInt32() > 0)
	            {
                    DPSDK_StopRealPlay(m_iSessionID, m_iVtCallRealMediaSessionID);
	            }
	            ClearVideoParam();
	        }
        }

        private void btnRejectVtCall_Click(object sender, EventArgs e)
        {
            if (m_bVtCallInvite)
            {
                DPSDK_VTCALL_REJECT_PARAM pParam = new DPSDK_VTCALL_REJECT_PARAM();
                pParam.szUserId = m_struVtCallInviteNotify.szUserId;
                pParam.iCallId = m_struVtCallInviteNotify.iCallId;
                pParam.iDlgId = m_struVtCallInviteNotify.iDlgId;
                pParam.iTid = m_struVtCallInviteNotify.iTId;

                DPSDK_RES iRet = DPSDK_RejectVtCall(m_iSessionID, ref pParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    this.btnVtCallStartVtCall.Text = AppData._CS("RejectVtCall success.");
                }
                else
                {
                    this.btnVtCallStartVtCall.Text = AppData._CS("RejectVtCall failed, iRet = %1", iRet.ToString());
                }
                m_bVtCallInvite = false;
            }
        }

        private void ClearVideoParam()
        {
            m_iVtCallMediaSessionID = IntPtr.Zero;
            m_iVtCallRealMediaSessionID = IntPtr.Zero;
            this.btnVtCallStartVtCall.Text = AppData._CS("StartVtCall");
        }

        private void trackBarVtCallVolume_ValueChanged(object sender, EventArgs e)
        {
            labelVtCallVolume.Text = trackBarVtCallVolume.Value.ToString();
        }

        private void btnVtCallOpenSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_OpenSound(m_iSessionID, m_iVtCallMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textVtCallHandleResult.Text = AppData._CS("Open Sound success.");
            }
            else
            {
                textVtCallHandleResult.Text = AppData._CS("Open Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnVtCallCloseSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_CloseSound(m_iSessionID, m_iVtCallMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textVtCallHandleResult.Text = AppData._CS("Close Sound success.");
            }
            else
            {
                textVtCallHandleResult.Text = AppData._CS("Close Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnVtCallOpenShareSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_OpenSoundShare(m_iSessionID, m_iVtCallMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textVtCallHandleResult.Text = AppData._CS("Open Share Sound success.");
            }
            else
            {
                textVtCallHandleResult.Text = AppData._CS("Open Share Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnVtCallCloseShareSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_CloseSoundShare(m_iSessionID, m_iVtCallMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textVtCallHandleResult.Text = AppData._CS("Close Share Sound success.");
            }
            else
            {
                textVtCallHandleResult.Text = AppData._CS("Close Share Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnVtCallIsOpenSound_Click(object sender, EventArgs e)
        {
            IntPtr pIsOpenSound = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_IsOpenSoundState(m_iSessionID, m_iVtCallMediaSessionID, ref pIsOpenSound);
            if (iRet == DPSDK_SUCCESS)
            {
                textVtCallHandleResult.Text = AppData._CS("Is Open Sound State success. bIsOpen = %1", pIsOpenSound.ToString());
            }
            else
            {
                textVtCallHandleResult.Text = AppData._CS("Is Open Sound State failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnVtCallSetVolumn_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_SetVolume(m_iSessionID, m_iVtCallMediaSessionID, (uint)trackBarVtCallVolume.Value);
            if (iRet == DPSDK_SUCCESS)
            {
                textVtCallHandleResult.Text = AppData._CS("Set Volume success.");
            }
            else
            {
                textVtCallHandleResult.Text = AppData._CS("Set Volume failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnVtCallGetVolumn_Click(object sender, EventArgs e)
        {
            IntPtr pVolume = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetVolume(m_iSessionID, m_iVtCallMediaSessionID, ref pVolume);
            if (iRet == DPSDK_SUCCESS)
            {
                textVtCallHandleResult.Text = AppData._CS("Get Volume success. iVolume = %1", pVolume.ToString());
                labelVtCallVolume.Text = pVolume.ToString();
                trackBarVtCallVolume.Value = pVolume.ToInt32();
            }
            else
            {
                textVtCallHandleResult.Text = AppData._CS("Get Volume failed, iRet = %1", iRet.ToString());
            }
        }
    }
}