public string GetAvailableTrips1(int sourceId, int destinationId, String dateofjourney, Int16 resultSetIndex, String ConsumerKey, String ConsumerSecret)
        {
            try
            {

                ClientAPIList objClientAPIList = new ClientAPIList();

                BusesAvailabilityResponse objBusesAvailabilityResponse = new BusesAvailabilityResponse();

                //Check if Cache already contains the Providers API List
                //Else get it from database
                // if (HttpContext.Current.Cache["ClientAPI-" + ConsumerKey] == null)
                GetAPIProvidersList(ConsumerKey, ConsumerSecret);

                objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];

                //Check if atleast one provider is accessible
                if (objClientAPIList != null && objClientAPIList.Count > 0)
                {
                    BusesSearchFilter objBusesSearchFilter = new BusesSearchFilter();
                    for (int i = 0; i < objClientAPIList.Count; i++)
                    {

                        objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[i], sourceId, destinationId, dateofjourney);
                        //check if resultset index exceeds accessible providers list call the requested api
                        if (resultSetIndex > 0 && resultSetIndex <= objClientAPIList.Count)
                        {
                            //Check if request has valid source and destination ids
                            if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                            {
                                //check if valid available trips are fetched
                                try
                                {

                                    objBusesAvailabilityResponse.availableTrips = GetAvailableTripsByProviderName(objClientAPIList[i], objBusesSearchFilter);
                                    objBusesAvailabilityResponse.responseStatus = HttpStatusCode.OK;

                                }
                                catch (Exception ex)
                                {
                                    //return the exception
                                    objBusesAvailabilityResponse.availableTrips = null;
                                    objBusesAvailabilityResponse.responseStatus = HttpStatusCode.BadRequest;
                                    objBusesAvailabilityResponse.message = ex.Message;
                                }
                            }
                            else
                            {
                                //objBusesAvailabilityResponse.availableTrips = "No results found.";
                                // objBusesAvailabilityResponse.responseStatus = HttpStatusCode.NoContent;
                                objBusesAvailabilityResponse.availableTrips = null;
                            }
                            objBusesAvailabilityResponse.providersCount = objClientAPIList.Count - resultSetIndex;
                        }
                        else
                        {
                            //objBusesAvailabilityResponse.availableTrips = "Invalid request. Please correct resultSetIndex value";
                            objBusesAvailabilityResponse.providersCount = 0;
                            objBusesAvailabilityResponse.responseStatus = HttpStatusCode.BadRequest;
                        }

                        if (objBusesAvailabilityResponse.availableTrips != null)
                        {
                            if (HttpContext.Current.Session["AvailResponse"] == null)
                            {
                                HttpContext.Current.Session["AvailResponse"] = objBusesAvailabilityResponse;
                                bus = trips(objBusesAvailabilityResponse.availableTrips);
                                //HttpContext.Current.Session["AvailResponseTrips"] = bus;
                                //bus = trips(bus);
                            }
                            else
                            {
                                //bus = trips((AvailableTrips)HttpContext.Current.Session["AvailResponseTrips"]);
                                bus = trips(objBusesAvailabilityResponse.availableTrips);
                            }
                        }
                    }
                    /*** Uncomment this if available trips of all providers should be fetched in one go ***/

                }
                else
                {

                    objBusesAvailabilityResponse.providersCount = 0;
                    objBusesAvailabilityResponse.responseStatus = HttpStatusCode.NoContent;
                }

                objBusesAvailabilityResponse.availableTrips = bus;
                HttpContext.Current.Session["AvailResponse"] = objBusesAvailabilityResponse;
                return JsonConvert.SerializeObject(objBusesAvailabilityResponse);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public string GetAvailableTrips2(int sourceId, int destinationId, String dateofjourney, Int16 resultSetIndex, String ConsumerKey, String ConsumerSecret)
        {
            try
            {
                ClientAPIList objClientAPIList = new ClientAPIList();
                BusesAvailabilityResponse objBusesAvailabilityResponse = new BusesAvailabilityResponse();
                if (resultSetIndex != 2)
                {
                    HttpContext.Current.Session["AvailResponse"] = null;
                    HttpRuntime.Cache["bus"] = "";

                    //Check if Cache already contains the Providers API List
                    //Else get it from database
                    GetAPIProvidersList(ConsumerKey, ConsumerSecret);
                    objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];
                    //Check if atleast one provider is accessible
                    if (objClientAPIList != null && objClientAPIList.Count > 0)
                    {
                        BusesSearchFilter objBusesSearchFilter = new BusesSearchFilter();
                        threadcount = Convert.ToInt32(objClientAPIList.Count());
                        thread = new Thread[threadcount];
                        for (i = 0; i < thread.Count(); i++)
                        {
                            thread[i] = new Thread(() =>
                            {
                                if (objClientAPIList[i].ProviderName == "TICKETGOOSE")
                                {
                                    int T = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[T], sourceId, destinationId, dateofjourney);
                                    if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                                    {
                                        TSID = Convert.ToInt32(objBusesSearchFilter.SourceID);
                                        TDID = Convert.ToInt32(objBusesSearchFilter.DestinationID);
                                        TDate = objBusesSearchFilter.JourneyDate;
                                        TURL = objClientAPIList[T].APIURL;
                                        TConsumerKey = objClientAPIList[T].ConsumerKey;
                                        TCOnsumerSecretKey = objClientAPIList[T].ConsumerSecret;
                                        TPID = objClientAPIList[T].ProviderID;
                                        Tbus = Ticketgoose(TURL, TConsumerKey, TCOnsumerSecretKey, TPID, TSID, TDID, TDate);
                                        if (Tbus != null)
                                        {
                                            if (Tbus.Count() > 0)
                                            {
                                                bus = trips(Tbus);
                                                HttpRuntime.Cache["bus"] = Tbus;
                                                objBusesAvailabilityResponse.providersCount = 10;
                                            }

                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "MORNINGSTAR")
                                {
                                    int M = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[M], sourceId, destinationId, dateofjourney);
                                    if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                                    {
                                        MSID = Convert.ToInt32(objBusesSearchFilter.SourceID);
                                        MDID = Convert.ToInt32(objBusesSearchFilter.DestinationID);
                                        MDate = objBusesSearchFilter.JourneyDate;
                                        MURL = objClientAPIList[M].APIURL + "/";
                                        MConsumerKey = objClientAPIList[M].ConsumerKey;
                                        MCOnsumerSecretKey = objClientAPIList[M].ConsumerSecret;
                                        MPID = objClientAPIList[M].ProviderID;
                                        MProviderName = objClientAPIList[M].ProviderName;
                                        Tbus = MORNINGSTAR(MURL, MConsumerKey, MCOnsumerSecretKey, MPID, MProviderName, MSID, MDID, MDate);
                                        if (Tbus != null)
                                        {
                                            if (Tbus.Count() > 0)
                                            {
                                                bus = trips(Tbus);
                                            }

                                            // b = true;
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "BITLA")
                                {
                                    int B = i;
                                    bus = GETBITLROUTES(objBusesSearchFilter, objClientAPIList[B], sourceId, destinationId, dateofjourney);

                                }
                                else if (objClientAPIList[i].ProviderName == "KALLADA")
                                {
                                    int kD = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[kD], sourceId, destinationId, dateofjourney);
                                    if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                                    {
                                        KDSID = Convert.ToInt32(objBusesSearchFilter.SourceID);
                                        KDDID = Convert.ToInt32(objBusesSearchFilter.DestinationID);
                                        KDDate = objBusesSearchFilter.JourneyDate;
                                        KDURL = objClientAPIList[kD].APIURL;
                                        KDConsumerKey = objClientAPIList[kD].ConsumerKey;
                                        KDCOnsumerSecretKey = objClientAPIList[kD].ConsumerSecret;
                                        KDProviderName = objClientAPIList[kD].ProviderName;
                                        Kbus = KALLADA(KDURL, KDConsumerKey, KDCOnsumerSecretKey, KDProviderName, KDSID, KDDID, KDDate);
                                        if (Kbus != null)
                                        {
                                            if (Kbus.Count() > 0)
                                            {
                                                bus = trips(Kbus);
                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "SAIANJANA")
                                {
                                    int s = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[s], sourceId, destinationId, dateofjourney);
                                    if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                                    {
                                        SSID = Convert.ToInt32(objBusesSearchFilter.SourceID);
                                        SDID = Convert.ToInt32(objBusesSearchFilter.DestinationID);
                                        SDate = objBusesSearchFilter.JourneyDate;
                                        SURL = objClientAPIList[s].APIURL;
                                        SConsumerKey = objClientAPIList[s].ConsumerKey;
                                        SCOnsumerSecretKey = objClientAPIList[s].ConsumerSecret;
                                        SProviderName = objClientAPIList[s].ProviderName;
                                        Kbus = SAIANJANA(SURL, SConsumerKey, SCOnsumerSecretKey, SProviderName, SSID, SDID, SDate);
                                        if (Kbus != null)
                                        {
                                            if (Kbus.Count() > 0)
                                            {
                                                bus = trips(Kbus);

                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "SVR")
                                {
                                    int sv = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[sv], sourceId, destinationId, dateofjourney);
                                    if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                                    {
                                        SVSID = Convert.ToInt32(objBusesSearchFilter.SourceID);
                                        SVDID = Convert.ToInt32(objBusesSearchFilter.DestinationID);
                                        SVDate = objBusesSearchFilter.JourneyDate;
                                        SVURL = objClientAPIList[sv].APIURL;
                                        SVConsumerKey = objClientAPIList[sv].ConsumerKey;
                                        SVCOnsumerSecretKey = objClientAPIList[sv].ConsumerSecret;
                                        SVProviderName = objClientAPIList[sv].ProviderName;
                                        Kbus = GETSVR(SVURL, SVConsumerKey, SVCOnsumerSecretKey, SVProviderName, SVSID, SVDID, SVDate);
                                        if (Kbus != null)
                                        {
                                            if (Kbus.Count() > 0)
                                            {
                                                bus = trips(Kbus);
                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "RAJESH")
                                {
                                    int r = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[r], sourceId, destinationId, dateofjourney);
                                    if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                                    {
                                        RSID = Convert.ToInt32(objBusesSearchFilter.SourceID);
                                        RDID = Convert.ToInt32(objBusesSearchFilter.DestinationID);
                                        RDate = objBusesSearchFilter.JourneyDate;
                                        RURL = objClientAPIList[r].APIURL;
                                        RConsumerKey = objClientAPIList[r].ConsumerKey;
                                        RCOnsumerSecretKey = objClientAPIList[r].ConsumerSecret;
                                        RProviderName = objClientAPIList[r].ProviderName;
                                        Kbus = Rajesh(RURL, RConsumerKey, RCOnsumerSecretKey, RProviderName, RSID, RDID, RDate);
                                        if (Kbus != null)
                                        {
                                            if (Kbus.Count() > 0)
                                            {
                                                bus = trips(Kbus);
                                                //HttpRuntime.Cache["bus"] = Tbus;
                                            }

                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "KAVERI")
                                {
                                    int k = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[k], sourceId, destinationId, dateofjourney);
                                    if (objBusesSearchFilter.SourceID > 0 && objBusesSearchFilter.DestinationID > 0)
                                    {
                                        KSID = Convert.ToInt32(objBusesSearchFilter.SourceID);
                                        KDID = Convert.ToInt32(objBusesSearchFilter.DestinationID);
                                        KDate = objBusesSearchFilter.JourneyDate;
                                        KURL = objClientAPIList[i].APIURL;
                                        KConsumerKey = objClientAPIList[k].ConsumerKey;
                                        KCOnsumerSecretKey = objClientAPIList[k].ConsumerSecret;
                                        KProviderName = objClientAPIList[k].ProviderName;
                                        Kbus = KAVERI(KURL, KConsumerKey, KCOnsumerSecretKey, KProviderName, KSID, KDID, KDate);
                                        if (Kbus != null)
                                        {
                                            if (Kbus.Count() > 0)
                                            {
                                                bus = trips(Kbus);
                                                //HttpRuntime.Cache["Kbus"] = Kbus;

                                            }

                                        }
                                    }
                                }
                            });
                            // threat initialisation ends

                            if (c == true)
                            {
                                thread[i].Start();
                                thread[i].Join(500);
                                if (i == thread.Count() - 1)
                                {
                                    i = thread.Count() - 1;
                                    break;
                                }
                            }

                        }
                    }
                }

                if (resultSetIndex != 2)
                {

                Found:
                    int j = 0;
                    foreach (var thd in thread)
                    {
                        if (thd.IsAlive)
                        {
                            thd.Join(1000);
                            b = false;
                        }
                        else
                        {
                            j++;
                        }
                        if (j == threadcount)
                        {
                            b = true;
                            i = 0;
                            break;
                        }

                    }

                    if (c == true)
                    {
                        c = false;
                    }
                    if (b == false)
                    {
                        goto Found;
                    }
                    else
                    {
                        b = false;
                        //check if resultset index exceeds accessible providers list call the requested api
                        if (resultSetIndex > 0 && resultSetIndex <= objClientAPIList.Count)
                        {
                            try
                            {
                                // objBusesAvailabilityResponse.availableTrips = GetAvailableTripsByProviderName(objClientAPIList[i], objBusesSearchFilter);
                                objBusesAvailabilityResponse.responseStatus = HttpStatusCode.OK;

                            }
                            catch (Exception ex)
                            {
                                //return the exception
                                objBusesAvailabilityResponse.availableTrips = null;
                                objBusesAvailabilityResponse.responseStatus = HttpStatusCode.BadRequest;
                                objBusesAvailabilityResponse.message = ex.Message;
                            }
                            if (objBusesAvailabilityResponse.providersCount == 10)
                            {
                                objBusesAvailabilityResponse.providersCount = 0;
                            }
                            else
                            {
                                objBusesAvailabilityResponse.providersCount = objClientAPIList.Count - resultSetIndex;
                            }
                        }
                        else
                        {
                            objBusesAvailabilityResponse.providersCount = 0;
                            objBusesAvailabilityResponse.responseStatus = HttpStatusCode.BadRequest;
                        }
                    }
                }
                if (HttpContext.Current.Session["AvailResponse"] == null)
                {
                    objBusesAvailabilityResponse.availableTrips = bus;
                    HttpContext.Current.Session["AvailResponse"] = objBusesAvailabilityResponse;
                    HttpRuntime.Cache["bus"] = "";
                }
                else
                {
                bump:
                    if (HttpRuntime.Cache["bus"].ToString() != "")
                    {
                        Tbus = (AvailableTrips)HttpRuntime.Cache["bus"];

                        if (Tbus.Count() > 0)
                        {
                            BusesAvailabilityResponse busback = new BusesAvailabilityResponse();
                            Tbus = (AvailableTrips)HttpRuntime.Cache["bus"];
                            busback = (BusesAvailabilityResponse)HttpContext.Current.Session["AvailResponse"];
                            bus = trips((AvailableTrips)busback.availableTrips);
                            bus = trips(Tbus);
                            busback.availableTrips = bus;
                            busback.responseStatus = HttpStatusCode.OK;
                            busback.providersCount = 0;
                            HttpContext.Current.Session["AvailResponse"] = busback;
                            objBusesAvailabilityResponse.availableTrips = Tbus;
                            objBusesAvailabilityResponse.responseStatus = HttpStatusCode.OK;
                            objBusesAvailabilityResponse.providersCount = 0;
                            HttpRuntime.Cache["bus"] = "";

                        }

                    }
                    else
                    {
                        objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];
                        if (objClientAPIList[1].ProviderName == "TICKETGOOSE")
                        {
                            goto bump;
                        }
                        else
                        {

                        }
                    }

                }
                return JsonConvert.SerializeObject(objBusesAvailabilityResponse);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        private AvailableTrips Ticketgoose(string TAPIURL, string TConsumerKey, string TConsumerSecret, int TPID, int Tsourceid, int Tdestinationid, string Tdate)
        {
            BusesAvailabilityResponse objBusesAvailabilityResponse = new BusesAvailabilityResponse();
            BusesSearchFilter objBusesSearchFilter = new BusesSearchFilter();
            AvailableTrips objAvailableTrips = new AvailableTrips();

            TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
            objAvailableTrips = clsTicketGooseAPI.getTripListV2(Tsourceid
                                , Tdestinationid
                                , Tdate
                                , TAPIURL
                                , TConsumerKey
                                , TConsumerSecret, TPID);

            return objAvailableTrips;
        }
        public string GetAvailableTrips(int sourceId, int destinationId, String dateofjourney, Int16 resultSetIndex, String ConsumerKey, String ConsumerSecret)
        {
            try
            {
                ClientAPIList objClientAPIList = new ClientAPIList();
                BusesAvailabilityResponse objBusesAvailabilityResponse = new BusesAvailabilityResponse();
                if (resultSetIndex != 2)
                {
                    HttpContext.Current.Session["AvailResponse"] = null;
                    HttpRuntime.Cache["bus"] = "";

                    //Check if Cache already contains the Providers API List
                    //Else get it from database
                    GetAPIProvidersList(ConsumerKey, ConsumerSecret);
                    objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];
                    //Check if atleast one provider is accessible
                    if (objClientAPIList != null && objClientAPIList.Count > 0)
                    {
                        BusesSearchFilter objBusesSearchFilter = new BusesSearchFilter();
                        threadcount = Convert.ToInt32(objClientAPIList.Count());
                        thread = new Thread[threadcount];
                        for (i = 0; i < thread.Count(); i++)
                        {
                            thread[i] = new Thread(() =>
                            {
                                if (objClientAPIList[i].ProviderName == "TICKETGOOSE")
                                {
                                    int T = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[T], sourceId, destinationId, dateofjourney);
                                    if (Tsource_ids.ToString() != "" && TDestination_Ids.ToString() != "")
                                    {
                                        string[] Tsu = Tsource_ids.Split(',');
                                        string[] Tdu = TDestination_Ids.Split(',');
                                        string[] TSdr = Tsu.Distinct().ToArray();
                                        string[] TDdr = Tdu.Distinct().ToArray();
                                        foreach (string Tsr in TSdr)
                                        {
                                            foreach (string Tdr in TDdr)
                                            {
                                                TSID = Convert.ToInt32(Tsr);
                                                TDID = Convert.ToInt32(Tdr);
                                                TDate = objBusesSearchFilter.JourneyDate;
                                                TURL = objClientAPIList[T].APIURL;
                                                TConsumerKey = objClientAPIList[T].ConsumerKey;
                                                TCOnsumerSecretKey = objClientAPIList[T].ConsumerSecret;
                                                TPID = objClientAPIList[T].ProviderID;
                                                Tbus = Ticketgoose(TURL, TConsumerKey, TCOnsumerSecretKey, TPID, TSID, TDID, TDate);
                                                if (Tbus != null)
                                                {
                                                    if (Tbus.Count() > 0)
                                                    {
                                                        bus = trips(Tbus);
                                                        HttpRuntime.Cache["bus"] = Tbus;
                                                        objBusesAvailabilityResponse.providersCount = 10;
                                                    }

                                                }

                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "MORNINGSTAR")
                                {
                                    int M = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[M], sourceId, destinationId, dateofjourney);
                                    if (Msource_ids.ToString() != "" && MDestination_Ids.ToString() != "")
                                    {
                                        string[] Msu = Msource_ids.Split(',');
                                        string[] Mdu = MDestination_Ids.Split(',');
                                        string[] MSdr = Msu.Distinct().ToArray();
                                        string[] MDdr = Mdu.Distinct().ToArray();
                                        foreach (string Msr in MSdr)
                                        {
                                            foreach (string Mdr in MDdr)
                                            {
                                                MSID = Convert.ToInt32(Msr);
                                                MDID = Convert.ToInt32(Mdr);
                                                MDate = objBusesSearchFilter.JourneyDate;
                                                MURL = objClientAPIList[M].APIURL + "/";
                                                MConsumerKey = objClientAPIList[M].ConsumerKey;
                                                MCOnsumerSecretKey = objClientAPIList[M].ConsumerSecret;
                                                MPID = objClientAPIList[M].ProviderID;
                                                MProviderName = objClientAPIList[M].ProviderName;
                                                Tbus = MORNINGSTAR(MURL, MConsumerKey, MCOnsumerSecretKey, MPID, MProviderName, MSID, MDID, MDate);
                                                if (Tbus != null)
                                                {
                                                    if (Tbus.Count() > 0)
                                                    {
                                                        bus = trips(Tbus);
                                                    }
                                                }

                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "BITLA")
                                {
                                    //objBusesSearchFilter.SourceID = Sdr.ToString() == "" ? 0 : int.Parse(Sdr.ToString());
                                    //objBusesSearchFilter.DestinationID = Ddr.ToString() == "" ? 0 : Convert.ToInt32(Ddr.ToString());
                                    //objBusesSearchFilter.JourneyDate = dateofjourney;
                                    int B = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[B], sourceId, destinationId, dateofjourney);
                                    if (source_ids.ToString() != "" && Destination_Ids.ToString() != "")
                                    {
                                        string[] su = source_ids.Split(',');
                                        string[] du = Destination_Ids.Split(',');
                                        string[] SSdr = su.Distinct().ToArray();
                                        string[] DDdr = du.Distinct().ToArray();
                                        foreach (string Sdr in SSdr)
                                        {
                                            foreach (string Ddr in DDdr)
                                            {
                                                BSID = Convert.ToInt32(Sdr);
                                                BDID = Convert.ToInt32(Ddr);
                                                BDate = objBusesSearchFilter.JourneyDate;
                                                BURL = objClientAPIList[B].APIURL + "/";
                                                BConsumerKey = objClientAPIList[B].ConsumerKey;
                                                BCOnsumerSecretKey = objClientAPIList[B].ConsumerSecret;
                                                BPID = objClientAPIList[B].ProviderID;
                                                BProviderName = objClientAPIList[B].ProviderName;
                                                Tbus = BITLA(BURL, BConsumerKey, BCOnsumerSecretKey, BPID, BProviderName, BSID, BDID, BDate);
                                                if (Tbus != null)
                                                {
                                                    if (Tbus.Count() > 0)
                                                    {
                                                        bus = trips(Tbus);
                                                    }
                                                }

                                            }
                                        }
                                    }

                                }
                                else if (objClientAPIList[i].ProviderName == "KALLADA")
                                {
                                    int kD = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[kD], sourceId, destinationId, dateofjourney);
                                    if (KDsource_ids.ToString() != "" && KDDestination_Ids.ToString() != "")
                                    {
                                        string[] KDsu = KDsource_ids.Split(',');
                                        string[] KDdu = KDDestination_Ids.Split(',');
                                        string[] KSdr = KDsu.Distinct().ToArray();
                                        string[] KDdr = KDdu.Distinct().ToArray();
                                        foreach (string Ksr in KSdr)
                                        {
                                            foreach (string Kdr in KDdr)
                                            {

                                                KDSID = Convert.ToInt32(Ksr);
                                                KDDID = Convert.ToInt32(Kdr);
                                                KDDate = objBusesSearchFilter.JourneyDate;
                                                KDURL = objClientAPIList[kD].APIURL;
                                                KDConsumerKey = objClientAPIList[kD].ConsumerKey;
                                                KDCOnsumerSecretKey = objClientAPIList[kD].ConsumerSecret;
                                                KDProviderName = objClientAPIList[kD].ProviderName;
                                                Kbus = KALLADA(KDURL, KDConsumerKey, KDCOnsumerSecretKey, KDProviderName, KDSID, KDDID, KDDate);
                                                if (Kbus != null)
                                                {
                                                    if (Kbus.Count() > 0)
                                                    {
                                                        bus = trips(Kbus);
                                                    }
                                                }

                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "SAIANJANA")
                                {
                                    int s = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[s], sourceId, destinationId, dateofjourney);
                                    if (SSsource_ids.ToString() != "" && SSDestination_Ids.ToString() != "")
                                    {
                                        string[] SSsu = SSsource_ids.Split(',');
                                        string[] SSdu = SSDestination_Ids.Split(',');
                                        string[] SSdr = SSsu.Distinct().ToArray();
                                        string[] SDdr = SSdu.Distinct().ToArray();
                                        foreach (string Ssr in SSdr)
                                        {
                                            foreach (string Sdr in SDdr)
                                            {
                                                SSID = Convert.ToInt32(Ssr);
                                                SDID = Convert.ToInt32(Sdr);
                                                SDate = objBusesSearchFilter.JourneyDate;
                                                SURL = objClientAPIList[s].APIURL;
                                                SConsumerKey = objClientAPIList[s].ConsumerKey;
                                                SCOnsumerSecretKey = objClientAPIList[s].ConsumerSecret;
                                                SProviderName = objClientAPIList[s].ProviderName;
                                                Kbus = SAIANJANA(SURL, SConsumerKey, SCOnsumerSecretKey, SProviderName, SSID, SDID, SDate);
                                                if (Kbus != null)
                                                {
                                                    if (Kbus.Count() > 0)
                                                    {
                                                        bus = trips(Kbus);

                                                    }
                                                }

                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "SVR")
                                {
                                    int sv = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[sv], sourceId, destinationId, dateofjourney);
                                    if (Ssource_ids.ToString() != "" && SDestination_Ids.ToString() != "")
                                    {
                                        string[] sssu = Ssource_ids.Split(',');
                                        string[] ssdu = SDestination_Ids.Split(',');
                                        string[] SSSdr = sssu.Distinct().ToArray();
                                        string[] SSDdr = ssdu.Distinct().ToArray();
                                        foreach (string SSsr in SSSdr)
                                        {
                                            foreach (string SSdr in SSDdr)
                                            {

                                                SVSID = Convert.ToInt32(SSsr);
                                                SVDID = Convert.ToInt32(SSdr);
                                                SVDate = objBusesSearchFilter.JourneyDate;
                                                SVURL = objClientAPIList[sv].APIURL;
                                                SVConsumerKey = objClientAPIList[sv].ConsumerKey;
                                                SVCOnsumerSecretKey = objClientAPIList[sv].ConsumerSecret;
                                                SVProviderName = objClientAPIList[sv].ProviderName;
                                                Kbus = GETSVR(SVURL, SVConsumerKey, SVCOnsumerSecretKey, SVProviderName, SVSID, SVDID, SVDate);
                                                if (Kbus != null)
                                                {
                                                    if (Kbus.Count() > 0)
                                                    {
                                                        bus = trips(Kbus);
                                                    }
                                                }

                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "RAJESH")
                                {
                                    int r = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[r], sourceId, destinationId, dateofjourney);
                                    if (Rsource_ids.ToString() != "" && RDestination_Ids.ToString() != "")
                                    {
                                        string[] Rsu = Rsource_ids.Split(',');
                                        string[] Rdu = RDestination_Ids.Split(',');
                                        string[] RSdr = Rsu.Distinct().ToArray();
                                        string[] RDdr = Rdu.Distinct().ToArray();
                                        foreach (string Rsr in RSdr)
                                        {
                                            foreach (string Rdr in RDdr)
                                            {
                                                RSID = Convert.ToInt32(Rsr);
                                                RDID = Convert.ToInt32(Rdr);
                                                RDate = objBusesSearchFilter.JourneyDate;
                                                RURL = objClientAPIList[r].APIURL;
                                                RConsumerKey = objClientAPIList[r].ConsumerKey;
                                                RCOnsumerSecretKey = objClientAPIList[r].ConsumerSecret;
                                                RProviderName = objClientAPIList[r].ProviderName;
                                                Kbus = Rajesh(RURL, RConsumerKey, RCOnsumerSecretKey, RProviderName, RSID, RDID, RDate);
                                                if (Kbus != null)
                                                {
                                                    if (Kbus.Count() > 0)
                                                    {
                                                        bus = trips(Kbus);
                                                        //HttpRuntime.Cache["bus"] = Tbus;
                                                    }

                                                }

                                            }
                                        }
                                    }
                                }
                                else if (objClientAPIList[i].ProviderName == "KAVERI")
                                {
                                    int k = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[k], sourceId, destinationId, dateofjourney);
                                    if (Ksource_ids.ToString() != "" && KDestination_Ids.ToString() != "")
                                    {
                                        string[] Ksu = Ksource_ids.Split(',');
                                        string[] Kdu = KDestination_Ids.Split(',');
                                        string[] KSdr = Ksu.Distinct().ToArray();
                                        string[] KDdr = Kdu.Distinct().ToArray();
                                        foreach (string Ksr in KSdr)
                                        {
                                            foreach (string Kdr in KDdr)
                                            {
                                                KSID = Convert.ToInt32(Ksr);
                                                KDID = Convert.ToInt32(Kdr);
                                                KDate = objBusesSearchFilter.JourneyDate;
                                                KURL = objClientAPIList[i].APIURL;
                                                KConsumerKey = objClientAPIList[k].ConsumerKey;
                                                KCOnsumerSecretKey = objClientAPIList[k].ConsumerSecret;
                                                KProviderName = objClientAPIList[k].ProviderName;
                                                Kbus = KAVERI(KURL, KConsumerKey, KCOnsumerSecretKey, KProviderName, KSID, KDID, KDate);
                                                if (Kbus != null)
                                                {
                                                    if (Kbus.Count() > 0)
                                                    {
                                                        bus = trips(Kbus);
                                                        //HttpRuntime.Cache["Kbus"] = Kbus;

                                                    }

                                                }

                                            }
                                        }
                                    }
                                }
                                //easy bus 17-05-2013
                                else if (objClientAPIList[i].ProviderName == "EASYBUS")
                                {
                                    int E = i;
                                    objBusesSearchFilter = GetCityIDsOfProviders(objBusesSearchFilter, objClientAPIList[E], sourceId, destinationId, dateofjourney);
                                    if (Esource_ids.ToString() != "" && EDestination_Ids.ToString() != "")
                                    {
                                        string[] Esu = Esource_ids.Split(',');
                                        string[] Edu = EDestination_Ids.Split(',');
                                        string[] ESdr = Esu.Distinct().ToArray();
                                        string[] EDdr = Edu.Distinct().ToArray();
                                        foreach (string Esr in ESdr)
                                        {
                                            foreach (string Edr in EDdr)
                                            {

                                                ESID = Convert.ToInt32(Esr);
                                                EDID = Convert.ToInt32(Edr);
                                                EDate = dateofjourney;
                                                EURL = objClientAPIList[E].APIURL;
                                                EConsumerKey = objClientAPIList[E].ConsumerKey;
                                                ECOnsumerSecretKey = objClientAPIList[E].ConsumerSecret;
                                                EProviderName = objClientAPIList[E].ProviderName;
                                                Kbus = EASY(EURL, EConsumerKey, ECOnsumerSecretKey, EProviderName, ESID, EDID, EDate);
                                                if (Kbus != null)
                                                {
                                                    if (Kbus.Count() > 0)
                                                    {
                                                        bus = trips(Kbus);
                                                    }
                                                }

                                            }
                                        }
                                    }

                                }
                            });
                            // threat initialisation ends

                            if (c == true)
                            {
                                thread[i].Start();
                                thread[i].Join(500);
                                if (i == thread.Count() - 1)
                                {
                                    i = thread.Count() - 1;
                                    break;
                                }
                            }

                        }
                    }
                }

                if (resultSetIndex != 2)
                {

                Found:
                    int j = 0;
                    foreach (var thd in thread)
                    {
                        if (thd.IsAlive)
                        {
                            thd.Join(1000);
                            b = false;
                        }
                        else
                        {
                            j++;
                        }
                        if (j == threadcount)
                        {
                            b = true;
                            i = 0;
                            break;
                        }

                    }

                    if (c == true)
                    {
                        c = false;
                    }
                    if (b == false)
                    {
                        goto Found;
                    }
                    else
                    {
                        b = false;
                        //check if resultset index exceeds accessible providers list call the requested api
                        if (resultSetIndex > 0 && resultSetIndex <= objClientAPIList.Count)
                        {
                            try
                            {
                                // objBusesAvailabilityResponse.availableTrips = GetAvailableTripsByProviderName(objClientAPIList[i], objBusesSearchFilter);
                                objBusesAvailabilityResponse.responseStatus = HttpStatusCode.OK;

                            }
                            catch (Exception ex)
                            {
                                //return the exception
                                objBusesAvailabilityResponse.availableTrips = null;
                                objBusesAvailabilityResponse.responseStatus = HttpStatusCode.BadRequest;
                                objBusesAvailabilityResponse.message = ex.Message;
                            }
                            if (objBusesAvailabilityResponse.providersCount == 10)
                            {
                                objBusesAvailabilityResponse.providersCount = 0;
                            }
                            else
                            {
                                objBusesAvailabilityResponse.providersCount = 0;
                                //  objBusesAvailabilityResponse.providersCount = objClientAPIList.Count - resultSetIndex;
                            }
                        }
                        else
                        {
                            objBusesAvailabilityResponse.providersCount = 0;
                            objBusesAvailabilityResponse.responseStatus = HttpStatusCode.BadRequest;
                        }
                    }
                }
                if (HttpContext.Current.Session["AvailResponse"] == null)
                {
                    objBusesAvailabilityResponse.availableTrips = bus;
                    HttpContext.Current.Session["AvailResponse"] = objBusesAvailabilityResponse;
                    HttpRuntime.Cache["bus"] = "";
                }
                else
                {
                bump:
                    if (HttpRuntime.Cache["bus"].ToString() != "")
                    {
                        Tbus = (AvailableTrips)HttpRuntime.Cache["bus"];

                        if (Tbus.Count() > 0)
                        {
                            BusesAvailabilityResponse busback = new BusesAvailabilityResponse();
                            Tbus = (AvailableTrips)HttpRuntime.Cache["bus"];
                            busback = (BusesAvailabilityResponse)HttpContext.Current.Session["AvailResponse"];
                            bus = trips((AvailableTrips)busback.availableTrips);
                            bus = trips(Tbus);
                            busback.availableTrips = bus;
                            busback.responseStatus = HttpStatusCode.OK;
                            busback.providersCount = 0;
                            HttpContext.Current.Session["AvailResponse"] = busback;
                            objBusesAvailabilityResponse.availableTrips = Tbus;
                            objBusesAvailabilityResponse.responseStatus = HttpStatusCode.OK;
                            objBusesAvailabilityResponse.providersCount = 0;
                            HttpRuntime.Cache["bus"] = "";

                        }

                    }
                    else
                    {
                        objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];
                        if (objClientAPIList[1].ProviderName == "TICKETGOOSE")
                        {
                            goto bump;
                        }
                        else
                        {

                        }
                    }

                }
                return JsonConvert.SerializeObject(objBusesAvailabilityResponse);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// Method to get available trips for given provider
        /// </summary>
        /// <param name="ProviderName"></param>
        /// <param name="objClientAPIDetails"></param>
        /// <returns>Returns JSON string with list of available trips</returns>
        private SeatsInfo GetTripDetailsByProviderName(ClientAPIDetails objClientAPIDetails, BusesSearchFilter objBusesSearchFilter, String tripId, String markUpFare, String SeatLayoutId)
        {
            SeatsInfo objSeatsInfo = new SeatsInfo();
            try
            {
                switch (objClientAPIDetails.ProviderName)
                {

                    case "ABHIBUS":
                    case "SVR":
                    case "KALLADA":
                    case "KAVERI":
                    case "RAJESH":
                    case "SAIANJANA":
                        AbhibusAPI clsAbhibusAPI = new AbhibusAPI();
                        objSeatsInfo = clsAbhibusAPI.getBusSeatLayout(objBusesSearchFilter.SourceID
                                            , objBusesSearchFilter.DestinationID
                                            , objBusesSearchFilter.JourneyDate
                                            , tripId
                                            , "0"
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey);
                        break;
                    case "BITLA":
                    case "MORNINGSTAR":
                        BitlaAPI clsBitlaAPI = new BitlaAPI();
                        objSeatsInfo = clsBitlaAPI.getServiceDetails(tripId
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey);

                        break;
                    case "TICKETGOOSE":
                        TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
                        objSeatsInfo = clsTicketGooseAPI.getTripDetailsV2(objBusesSearchFilter.SourceID
                                            , objBusesSearchFilter.DestinationID
                                            , objBusesSearchFilter.JourneyDate
                                            , tripId
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey
                                            , objClientAPIDetails.ConsumerSecret);
                        break;
                    //easy bus 18-05-2013
                    case "EASYBUS":
                        EasybusAPI clsEasyBusAPI = new EasybusAPI();
                        objSeatsInfo = clsEasyBusAPI.getLayoutDetails(objBusesSearchFilter.SourceID
                                            , objBusesSearchFilter.DestinationID
                                            , objBusesSearchFilter.JourneyDate
                                            , tripId
                                            , SeatLayoutId
                                            , markUpFare
                                            , "0"
                                            , objClientAPIDetails.APIURL
                                            , objClientAPIDetails.ConsumerKey);
                        break;
                    default:
                        break;
                }
                objSeatsInfo.providerName = objClientAPIDetails.ProviderName;
            }
            catch (Exception ex)
            {
                //to do
                //Log Exception
            }
            return objSeatsInfo;
        }
        private BusesSearchFilter GetCityIDsOfProviders(BusesSearchFilter objBusesSearchFilter, ClientAPIDetails objClientAPIDetails,
            int sourceId, int destinationId, String dateofjourney)
        {
            DataSet dsProviders = new DataSet();
            if (objClientAPIDetails.ProviderName != "MORNINGSTAR")
            {
                ClsBAL bal = new ClsBAL();
                bal.ProviderID = objClientAPIDetails.ProviderID;
                bal.sourceId = sourceId;
                bal.destionationId = destinationId;
                dsProviders = bal.GetAPIID();

                //DataTable DtRSources = new DataTable();
                //DataSet DsRSources = new DataSet();
                //DsRSources.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/" + "SourcesAPI.xml"));
                //IEnumerable<DataRow> API_Source_ID = from i in DsRSources.Tables[0].AsEnumerable()
                //                                     where i.Field<string>("Source_ID") == sourceId.ToString() && i.Field<string>("Provider_Id") == objClientAPIDetails.ProviderID.ToString()
                //                                     select i;
                //if (API_Source_ID.Count() > 0)
                //{
                //    DtRSources = API_Source_ID.CopyToDataTable();
                //    DataSet ds = new DataSet();
                //    ds.Tables.Add(DtRSources);

                //}

            }
            else
            {
                ClsBAL bal = new ClsBAL();
                bal.ProviderID = 102;
                bal.sourceId = sourceId;
                bal.destionationId = destinationId;
                dsProviders = bal.GetAPIID();
            }

            //DataSet dsProviders = ExecuteDataSet("SELECT " + objClientAPIDetails.ProviderName + " FROM dbo.tbl_Sources WHERE Source_ID = '" + sourceId + "' " +
            //                                       "SELECT " + objClientAPIDetails.ProviderName + " FROM dbo.tbl_Sources WHERE Source_ID = '" + destinationId + "'");

            if (dsProviders.Tables[0].Rows.Count > 0 && dsProviders.Tables[1].Rows.Count > 0)
            {
            if (objClientAPIDetails.ProviderName == "BITLA")
            {
                foreach (DataRow bsdr in dsProviders.Tables[0].Rows)
                {
                    if (source_ids.ToString() == "")
                    {
                        source_ids = bsdr[0].ToString();
                    }
                    else
                    {
                        source_ids = source_ids + "," + bsdr[0].ToString();
                    }
                }
                foreach (DataRow bddr in dsProviders.Tables[1].Rows)
                {
                    if (Destination_Ids.ToString() == "")
                    {
                        Destination_Ids = bddr[0].ToString();
                    }
                    else
                    {
                        Destination_Ids = Destination_Ids + "," + bddr[0].ToString();
                    }
                }
            }
            else if (objClientAPIDetails.ProviderName == "TICKETGOOSE")
            {
                foreach (DataRow tsdr in dsProviders.Tables[0].Rows)
                {
                    if (Tsource_ids.ToString() == "")
                    {
                        Tsource_ids = tsdr[0].ToString();
                    }
                    else
                    {
                        Tsource_ids = Tsource_ids + "," + tsdr[0].ToString();
                    }
                }
                foreach (DataRow tddr in dsProviders.Tables[1].Rows)
                {
                    if (TDestination_Ids.ToString() == "")
                    {
                        TDestination_Ids = tddr[0].ToString();
                    }
                    else
                    {
                        TDestination_Ids = TDestination_Ids + "," + tddr[0].ToString();
                    }
                }
            }
            else if (objClientAPIDetails.ProviderName == "MORNINGSTAR")
            {
                foreach (DataRow msdr in dsProviders.Tables[0].Rows)
                {
                    if (Msource_ids.ToString() == "")
                    {
                        Msource_ids = msdr[0].ToString();
                    }
                    else
                    {
                        Msource_ids = Msource_ids + "," + msdr[0].ToString();
                    }
                }
                foreach (DataRow mddr in dsProviders.Tables[1].Rows)
                {
                    if (MDestination_Ids.ToString() == "")
                    {
                        MDestination_Ids = mddr[0].ToString();
                    }
                    else
                    {
                        MDestination_Ids = MDestination_Ids + "," + mddr[0].ToString();
                    }
                }
            }
            else if (objClientAPIDetails.ProviderName == "RAJESH")
            {
                foreach (DataRow rsdr in dsProviders.Tables[0].Rows)
                {
                    if (Rsource_ids.ToString() == "")
                    {
                        Rsource_ids = rsdr[0].ToString();
                    }
                    else
                    {
                        Rsource_ids = Rsource_ids + "," + rsdr[0].ToString();
                    }
                }
                foreach (DataRow rddr in dsProviders.Tables[1].Rows)
                {
                    if (RDestination_Ids.ToString() == "")
                    {
                        RDestination_Ids = rddr[0].ToString();
                    }
                    else
                    {
                        RDestination_Ids = RDestination_Ids + "," + rddr[0].ToString();
                    }
                }
            }
            else if (objClientAPIDetails.ProviderName == "KALLADA")
            {
                foreach (DataRow kdsdr in dsProviders.Tables[0].Rows)
                {
                    if (KDsource_ids.ToString() == "")
                    {
                        KDsource_ids = kdsdr[0].ToString();
                    }
                    else
                    {
                        KDsource_ids = KDsource_ids + "," + kdsdr[0].ToString();
                    }
                }
                foreach (DataRow kdddr in dsProviders.Tables[1].Rows)
                {
                    if (KDDestination_Ids.ToString() == "")
                    {
                        KDDestination_Ids = kdddr[0].ToString();
                    }
                    else
                    {
                        KDDestination_Ids = KDDestination_Ids + "," + kdddr[0].ToString();
                    }
                }
            }
            else if (objClientAPIDetails.ProviderName == "KAVERI")
            {
                foreach (DataRow ksdr in dsProviders.Tables[0].Rows)
                {
                    if (Ksource_ids.ToString() == "")
                    {
                        Ksource_ids = ksdr[0].ToString();
                    }
                    else
                    {
                        Ksource_ids = Ksource_ids + "," + ksdr[0].ToString();
                    }
                }
                foreach (DataRow kddr in dsProviders.Tables[1].Rows)
                {
                    if (KDestination_Ids.ToString() == "")
                    {
                        KDestination_Ids = kddr[0].ToString();
                    }
                    else
                    {
                        KDestination_Ids = KDestination_Ids + "," + kddr[0].ToString();
                    }
                }
            }
            else if (objClientAPIDetails.ProviderName == "SAIANJANA")
            {
                foreach (DataRow ssdr in dsProviders.Tables[0].Rows)
                {
                    if (SSsource_ids.ToString() == "")
                    {
                        SSsource_ids = ssdr[0].ToString();
                    }
                    else
                    {
                        SSsource_ids = SSsource_ids + "," + ssdr[0].ToString();
                    }
                }
                foreach (DataRow ssddr in dsProviders.Tables[1].Rows)
                {
                    if (SSDestination_Ids.ToString() == "")
                    {
                        SSDestination_Ids = ssddr[0].ToString();
                    }
                    else
                    {
                        SSDestination_Ids = SSDestination_Ids + "," + ssddr[0].ToString();
                    }
                }
            }
            else if (objClientAPIDetails.ProviderName == "SVR")
            {
                foreach (DataRow sdr in dsProviders.Tables[0].Rows)
                {
                    if (Ssource_ids.ToString() == "")
                    {
                        Ssource_ids = sdr[0].ToString();
                    }
                    else
                    {
                        Ssource_ids = Ssource_ids + "," + sdr[0].ToString();
                    }
                }
                foreach (DataRow sddr in dsProviders.Tables[1].Rows)
                {
                    if (SDestination_Ids.ToString() == "")
                    {
                        SDestination_Ids = sddr[0].ToString();
                    }
                    else
                    {
                        SDestination_Ids = SDestination_Ids + "," + sddr[0].ToString();
                    }
                }
            }
            //easybus
            else if (objClientAPIDetails.ProviderName == "EASYBUS")
            {
                foreach (DataRow Edr in dsProviders.Tables[0].Rows)
                {
                    if (Esource_ids.ToString() == "")
                    {
                        Esource_ids = Edr[0].ToString();
                        //Esource_ids = "38";
                    }
                    else
                    {
                        Esource_ids = Esource_ids + "," + Edr[0].ToString();
                    }
                }
                foreach (DataRow Eddr in dsProviders.Tables[1].Rows)
                {
                    if (EDestination_Ids.ToString() == "")
                    {
                        EDestination_Ids = Eddr[0].ToString();
                       // EDestination_Ids = "32";
                    }
                    else
                    {
                        EDestination_Ids = EDestination_Ids + "," + Eddr[0].ToString();
                    }
                }
                //EDestination_Ids=38+","+32;
            }

            objBusesSearchFilter.SourceID = 0;
            // objBusesSearchFilter.DestinationID = int.Parse(dsProviders.Tables[1].Rows[0][0].ToString());
            objBusesSearchFilter.DestinationID = 0;
            objBusesSearchFilter.JourneyDate = dateofjourney;
            }
            dsProviders.Dispose();
            return objBusesSearchFilter;
        }
        private AvailableTrips GETBITLROUTES(BusesSearchFilter objBusesSearchFilter, ClientAPIDetails objClientAPIDetails,
           int sourceId, int destinationId, String dateofjourney)
        {
            ClsBAL bal = new ClsBAL();
            bal.ProviderID = objClientAPIDetails.ProviderID;
            bal.sourceId = sourceId;
            bal.destionationId = destinationId;
            DataSet dsProviders = bal.GetAPIID();

            //DataSet dsProviders = ExecuteDataSet("SELECT " + objClientAPIDetails.ProviderName + " FROM dbo.tbl_Sources WHERE Source_ID = '" + sourceId + "' " +
            //                                       "SELECT " + objClientAPIDetails.ProviderName + " FROM dbo.tbl_Sources WHERE Source_ID = '" + destinationId + "'");

            if (dsProviders.Tables[0].Rows.Count > 0 && dsProviders.Tables[1].Rows.Count > 0)
            {
                if (objClientAPIDetails.ProviderName == "BITLA")
                {
                    for (int k = 0; k < dsProviders.Tables.Count; k++)
                    {

                        BSID = dsProviders.Tables[0].Rows[k][0].ToString() == "" ? 0 : int.Parse(dsProviders.Tables[0].Rows[k][0].ToString());
                        BDID = dsProviders.Tables[1].Rows[k][0].ToString() == "" ? 0 : Convert.ToInt32(dsProviders.Tables[1].Rows[k][0]);
                        if (BSID > 0 && BDID > 0)
                        {
                            BDate = dateofjourney;
                            BURL = objClientAPIDetails.APIURL + "/";
                            BConsumerKey = objClientAPIDetails.ConsumerKey;
                            BCOnsumerSecretKey = objClientAPIDetails.ConsumerSecret;
                            BPID = objClientAPIDetails.ProviderID;
                            BProviderName = objClientAPIDetails.ProviderName;
                            Tbus = BITLA(BURL, BConsumerKey, BCOnsumerSecretKey, BPID, BProviderName, BSID, BDID, BDate);
                            bus = trips(Tbus);
                        }
                    }
                }
            }

            dsProviders.Dispose();
            return bus;
        }
        /// <summary>
        /// Method to get available trips for given provider
        /// </summary>
        /// <param name="ProviderName"></param>
        /// <param name="objClientAPIDetails"></param>
        /// <returns>Returns JSON string with list of available trips</returns>
        private AvailableTrips GetAvailableTripsByProviderName(ClientAPIDetails objClientAPIDetails, BusesSearchFilter objBusesSearchFilter)
        {
            AvailableTrips objAvailableTrips = new AvailableTrips();
            List<object> objlist = new List<object>();
            switch (objClientAPIDetails.ProviderName)
            {

                case "ABHIBUS":
                case "SVR":
                case "KALLADA":
                case "KAVERI":
                case "RAJESH":
                case "SAIANJANA":
                    AbhibusAPI clsAbhibusAPI = new AbhibusAPI();
                    objAvailableTrips = clsAbhibusAPI.getBusAvailability(objBusesSearchFilter.SourceID
                                        , objBusesSearchFilter.DestinationID
                                        , objBusesSearchFilter.JourneyDate
                                        , 6
                                        , "0"
                                        , objClientAPIDetails.APIURL
                                        , objClientAPIDetails.ConsumerKey
                                        , objClientAPIDetails.ProviderName);
                    break;
                case "BITLA":
                case "MORNINGSTAR":
                    BitlaAPI clsBitlaAPI = new BitlaAPI();
                    objAvailableTrips = clsBitlaAPI.getAvailableRoutes(objBusesSearchFilter.SourceID
                                        , objBusesSearchFilter.DestinationID
                                        , objBusesSearchFilter.JourneyDate
                                        , objClientAPIDetails.APIURL
                                        , objClientAPIDetails.ConsumerKey, objClientAPIDetails.ProviderID);
                    break;
                case "TICKETGOOSE":
                    TicketGooseAPI clsTicketGooseAPI = new TicketGooseAPI();
                    objAvailableTrips = clsTicketGooseAPI.getTripListV2(objBusesSearchFilter.SourceID
                                        , objBusesSearchFilter.DestinationID
                                        , objBusesSearchFilter.JourneyDate
                                        , objClientAPIDetails.APIURL
                                        , objClientAPIDetails.ConsumerKey
                                        , objClientAPIDetails.ConsumerSecret, objClientAPIDetails.ProviderID);
                    break;
                //easybus
                case "EASYBUS":
                    EasybusAPI clsEasyBusAPI = new EasybusAPI();

                    break;
                default:
                    break;

            }

            return objAvailableTrips;
        }
        public string GetTripDetails(String tripId, int sourceId, int destinationId, String markUpFare, String SeatLayoutId, String journeyDate, String provider, String ConsumerKey, String ConsumerSecret)
        {
            try
            {
                if (HttpContext.Current.Cache["ClientAPI-" + ConsumerKey] == null)
                    GetAPIProvidersList(ConsumerKey, ConsumerSecret);

                ClientAPIList objClientAPIList = (ClientAPIList)HttpContext.Current.Cache["ClientAPI-" + ConsumerKey];
                ClientAPIDetails objClientAPIDetails = objClientAPIList.SingleOrDefault(element => element.ProviderName == provider); //objClientAPIList.ElementAt(resultSet - 1)
                BusesSearchFilter objBusesSearchFilter = new BusesSearchFilter(sourceId, destinationId, journeyDate);

                SeatsInfo objSeatsInfo = GetTripDetailsByProviderName(objClientAPIDetails, objBusesSearchFilter, tripId, markUpFare, SeatLayoutId);
                if (provider == "TICKETGOOSE")
                {
                    //objSeatsInfo = GetSeatLayoutTG(objSeatsInfo);
                    objSeatsInfo = TestGetSeatLayout(objSeatsInfo);
                }
                else if (provider == "BITLA" || provider == "MORNINGSTAR")
                {
                    //objSeatsInfo = GetSeatLayoutTG(objSeatsInfo);
                    objSeatsInfo = BITLAGetSeatLayout(objSeatsInfo);
                }
                //easy bus 18-05-2013
                else if (provider == "EASYBUS")
                {
                    objSeatsInfo = GetSeatLayout(objSeatsInfo);
                }
                else
                {
                    objSeatsInfo = GetSeatLayout(objSeatsInfo);
                }
                //return JsonConvert.SerializeObject(GetSeatLayout(objSeatsInfo));
                return JsonConvert.SerializeObject(objSeatsInfo);

            }
            catch (Exception ex)
            {
                throw ex;
                // throw new System.Web.Http.HttpResponseException(HttpStatusCode.Forbidden);
                //to do
                //Log Exception
            }
        }