public VideoChatSettings InitiateVideoChat(long conversationid, string message, long peerid, int messagetype)
        {
            try
            {
                int res = -1;
                List<VideoChatSettings> objcoll = new List<VideoChatSettings>();
                VideoChatSettings objvideo = null;
                string constr = ConfigurationManager.ConnectionStrings["HickConnectionString"].ConnectionString.ToString();
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = constr;
                    conn.Open();

                    //using (SqlCommand command = new SqlCommand("select * from Hick_VideoConversation_Log as l where ConversationId=" + conversationid + " and l.PeerId=" + peerid + " and l.MessageType=" + (int)MessageTypes.Video + " and l.Status=" + (int)ReadStatus.CallInitiated + "", conn))
                    using (SqlCommand command = new SqlCommand("sp_hick_FetchVideoConvToInitiate", conn))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@ConvId", conversationid);
                        command.Parameters.AddWithValue("@PeerId", peerid);
                        command.Parameters.AddWithValue("@MsgType", (int)MessageTypes.Video);
                        command.Parameters.AddWithValue("@Status", (int)ReadStatus.CallInitiated);

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    //var cmd = "UPDATE Hick_Conversation_log set Conversation_log=@msg, Conversation_date=@date where Conversation_Id=" + conversationid + " and peerid=" + peerid + " and Message_Type=" + (int)MessageTypes.Video + "";
                                    //using (SqlCommand command1 = new SqlCommand(cmd, conn))
                                    //{
                                    //    command1.Parameters.AddWithValue("@msg", message);
                                    //    command1.Parameters.AddWithValue("@date", DateTime.Now);
                                    //    res = command1.ExecuteNonQuery();
                                    //}
                                    objvideo = new VideoChatSettings();
                                    objvideo.EnableVideoChat = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableVideoChat"]);
                                    objvideo.FlashServerType = Convert.ToString(ConfigurationManager.AppSettings["FlashServerType"]).Trim();
                                    objvideo.FlashMediaServer = Convert.ToString(ConfigurationManager.AppSettings["FlashMediaServer"]).Trim();
                                    objvideo.BroadcastVideoWidth = Convert.ToString(ConfigurationManager.AppSettings["BroadcastVideoWidth"]).Trim();
                                    objvideo.BroadcastVideoheight = Convert.ToString(ConfigurationManager.AppSettings["BroadcastVideoheight"]).Trim();
                                    objvideo.BroadcastVideoGuid = Convert.ToString(reader["VideoId"]);
                                    objvideo.ParentVideoId = Convert.ToString(reader["ParentVideoId"]);
                                    objcoll.Add(objvideo);
                                }

                            }
                            if (objcoll.Count > 0)
                            {
                                for (int i = 0; i < objcoll.Count; i++)
                                {
                                    UpdateVideoCallStatus(conversationid, objcoll[i].BroadcastVideoGuid, String.IsNullOrEmpty(objcoll[i].ParentVideoId) ? string.Empty : objcoll[i].ParentVideoId, (int)ReadStatus.CallEnded, (int)MessageTypes.Video, 0);
                                }
                            }

                            //var cmd = "INSERT INTO Hick_VideoConversation_Log (ConversationId,VideoId,ConversationDate,PeerId,MessageType,Status) VALUES (@conid,@msg,@date,@curntuserid,@messagetype,@status)";
                            using (SqlCommand command2 = new SqlCommand("sp_hick_InsertVideoConversationLog", conn))
                            {
                                command2.CommandType = CommandType.StoredProcedure;

                                command2.Parameters.AddWithValue("@conid", conversationid);
                                command2.Parameters.AddWithValue("@msg", message);
                                command2.Parameters.AddWithValue("@date", DateTime.Now);
                                command2.Parameters.AddWithValue("@curntuserid", peerid);
                                command2.Parameters.AddWithValue("@messagetype", messagetype);
                                command2.Parameters.AddWithValue("@status", (int)ReadStatus.CallInitiated);
                                res = command2.ExecuteNonQuery();
                                if (res >= 0)
                                {
                                    objvideo = new VideoChatSettings();
                                    objvideo.EnableVideoChat = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableVideoChat"]);
                                    objvideo.FlashServerType = Convert.ToString(ConfigurationManager.AppSettings["FlashServerType"]).Trim();
                                    objvideo.FlashMediaServer = Convert.ToString(ConfigurationManager.AppSettings["FlashMediaServer"]).Trim();
                                    objvideo.BroadcastVideoWidth = Convert.ToString(ConfigurationManager.AppSettings["BroadcastVideoWidth"]).Trim();
                                    objvideo.BroadcastVideoheight = Convert.ToString(ConfigurationManager.AppSettings["BroadcastVideoheight"]).Trim();
                                    objvideo.BroadcastVideoGuid = message;
                                }
                            }
                        }
                    }


                }
                return objvideo;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public VideoChatSettings ReceiveVideo(long conversationid, long peerid, long currentuserid)
        {

            VideoChatSettings objvideo = new VideoChatSettings();
            try
            {

                string constr = ConfigurationManager.ConnectionStrings["HickConnectionString"].ConnectionString.ToString();
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = constr;
                    conn.Open();

                    //using (SqlCommand command = new SqlCommand("select top 1 * from Hick_VideoConversation_Log as l where ConversationId=" + conversationid + " and l.PeerId=" + peerid + " and l.MessageType=" + (int)MessageTypes.Video + " and l.Status=" + (int)ReadStatus.CallInitiated + " order by l.ConversationDate desc", conn))
                    using (SqlCommand command = new SqlCommand("sp_hick_FetchTopVidConvLog", conn))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@ConvId", conversationid);
                        command.Parameters.AddWithValue("@UserId", peerid);
                        command.Parameters.AddWithValue("@MsgType", (int)MessageTypes.Video);
                        command.Parameters.AddWithValue("@Status", (int)ReadStatus.CallInitiated);

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    objvideo.ReceiveVideoGuid = Convert.ToString(reader["VideoId"]);
                                    objvideo.EnableVideoChat = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableVideoChat"]);
                                    objvideo.FlashServerType = Convert.ToString(ConfigurationManager.AppSettings["FlashServerType"]).Trim();
                                    objvideo.FlashMediaServer = Convert.ToString(ConfigurationManager.AppSettings["FlashMediaServer"]).Trim();
                                    objvideo.BroadcastVideoWidth = Convert.ToString(ConfigurationManager.AppSettings["BroadcastVideoWidth"]).Trim();
                                    objvideo.BroadcastVideoheight = Convert.ToString(ConfigurationManager.AppSettings["BroadcastVideoheight"]).Trim();

                                }
                            }

                        }
                    }
                    //using (SqlCommand command = new SqlCommand("select top 1 * from Hick_VideoConversation_Log as l where ConversationId=" + conversationid + " and l.PeerId=" + currentuserid + " and l.MessageType=" + (int)MessageTypes.Video + " and l.Status=" + (int)ReadStatus.CallInitiated + " order by l.ConversationDate desc", conn))
                    using (SqlCommand command = new SqlCommand("sp_hick_FetchTopVidConvLog", conn))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@ConvId", conversationid);
                        command.Parameters.AddWithValue("@UserId", currentuserid);
                        command.Parameters.AddWithValue("@MsgType", (int)MessageTypes.Video);
                        command.Parameters.AddWithValue("@Status", (int)ReadStatus.CallInitiated);

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    objvideo.BroadcastVideoGuid = Convert.ToString(reader["VideoId"]);
                                }
                            }
                        }
                    }
                    if (objvideo != null)
                    {
                        if (!String.IsNullOrEmpty(objvideo.BroadcastVideoGuid) && !String.IsNullOrEmpty(objvideo.ReceiveVideoGuid))
                        {

                            UpdateParentVedioId(conversationid, objvideo.BroadcastVideoGuid, objvideo.ReceiveVideoGuid);
                            UpdateParentVedioId(conversationid, objvideo.ReceiveVideoGuid, objvideo.BroadcastVideoGuid);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return objvideo;
        }
        public VideoChatSettings BroadcastGroupVideo(long conversationid, string peerid, string groupId)
        {

            VideoChatSettings objvideo = new VideoChatSettings();
            //try
            //{
            //    string guid = Convert.ToString(Guid.NewGuid());
            //    //objvideo = InitiateVideoChat(conversationid, guid, peerid, (int)MessageTypes.Video);

            //}
            //catch (Exception)
            //{
            //    throw;
            //}
            return objvideo;
        }
        public VideoChatSettings BroadcastVideo(long conversationid, long peerid)
        {

            VideoChatSettings objvideo = new VideoChatSettings();
            try
            {
                string guid = Convert.ToString(Guid.NewGuid());
                objvideo = InitiateVideoChat(conversationid, guid, peerid, (int)MessageTypes.Video);

            }
            catch (Exception)
            {
                throw;
            }
            return objvideo;
        }