<?php

namespace App\Http\Controllers\API;

use Auth;
use App\User; 
use App\Album;
use App\AppAds;
use App\Artists;
use App\Category;
use App\HomeSections;
use App\Pages;
use App\Playlist;
use App\Reports;
use App\Settings;
use App\Slider;
use App\Songs;
use App\Suggestion;
use App\Watchlist; 
use App\PostViewsDownload;
use App\PostRatings;
use App\Favourite;
use App\PasswordReset;
use App\PaymentGateway;
use App\SubscriptionPlan;
use App\Transactions;
use App\UsersDeviceHistory;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image; 
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Session;
use URL;
use Illuminate\Support\Facades\Password;

require(base_path() . '/public/stripe-php/init.php'); 
 
require(base_path() . '/public/razorpay/vendor/autoload.php');
 
use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;


class AndroidApiController extends MainAPIController
{
    public function __construct()
    {
        $this->pagination_limit=getcong('pagination_limit')?getcong('pagination_limit'):10;
    }
      
    public function index()
    {   
          $response[] = array('msg' => "API",'success'=>'1'); 
        
        return \Response::json(array(            
            'VIDEO_STREAMING_APP' => $response,
            'status_code' => 200
        ));   
         
    }
    public function app_details()
    {   
        
        $get_data=checkSignSalt($_POST['data']);
 
        if(isset($get_data['user_id']) && $get_data['user_id']!='')
        {
            $user_id=$get_data['user_id'];        
            $user_info = User::getUserInfo($user_id);

           if(!empty($user_info))
           {
                if($user_info!='' AND $user_info->status==1)
                {
                    $user_status=true;
                }
                else
                {
                    $user_status=false;
                }
            }
            else
            {
               $user_status=false; 
            }
        }
        else
        {
            $user_status=false;
        }
 
        $settings = Settings::findOrFail('1'); 

        $default_language=$settings->default_language;
        $app_name=$settings->app_name;
        $app_email=$settings->app_email;
        $app_logo=\URL::to('/'.$settings->app_logo);
        $app_company=$settings->app_company?$settings->app_company:'';
        $app_website=$settings->app_website?$settings->app_website:'';
        $app_contact=$settings->app_contact?$settings->app_contact:'';
        $app_version=$settings->app_version?$settings->app_version:'';

        $facebook_link=$settings->facebook_link?$settings->facebook_link:'';
        $twitter_link=$settings->twitter_link?$settings->twitter_link:'';
        $instagram_link=$settings->instagram_link?$settings->instagram_link:'';
        $youtube_link=$settings->youtube_link?$settings->youtube_link:'';
        $google_play_link=$settings->google_play_link?$settings->google_play_link:'';
        $apple_store_link=$settings->apple_store_link?$settings->apple_store_link:'';
         

        $app_update_hide_show=$settings->app_update_hide_show;
        $app_update_version_code=$settings->app_update_version_code?$settings->app_update_version_code:'1';
        $app_update_desc=stripslashes($settings->app_update_desc);
        $app_update_link=$settings->app_update_link;
        $app_update_cancel_option=$settings->app_update_cancel_option;

        $song_download=$settings->song_download;

        $subscription_on_off=$settings->subscription_on_off;

          
        $app_package_name=$settings->app_package_name?$settings->app_package_name:"com.vpapps.onlinemp3";

        //Ad List
        $ads_list = AppAds::where('status','1')->orderby('id')->get();  
        
        if(count($ads_list) > 0)
        {
            foreach($ads_list as $ad_data)
            {
                    $ad_id= $ad_data->id;
                    $ads_name= $ad_data->ads_name; 
                    $ads_info= json_decode($ad_data->ads_info);                  
                    $status= $ad_data->status?"true":"false";;                 
                     
                    $ads_obj[]=array("ad_id"=>$ad_id,"ads_name"=>$ads_name,"ads_info"=>$ads_info,"status"=>$status);   
            }
        }
        else
        {
            $ads_obj= array();
        }

        //Page List
        $page_list = Pages::where('status','1')->orderby('page_order')->get();  
  
        foreach($page_list as $page_data)
        {
                $page_id= $page_data->id;
                $page_title= stripslashes($page_data->page_title); 
                $page_content= stripslashes($page_data->page_content);                  
                  
                $pages_obj[]=array("page_id"=>$page_id,"page_title"=>$page_title,"page_content"=>$page_content);   
        }

        /***********Save visitor user info start************/
        $user_ip=\Request::ip();                
        $os_name = isset($get_data['os_name'])?$get_data['os_name']:"";   
        $browser_name = '';    
 
        save_visitor_analytics_info($user_ip,$os_name,$browser_name);

        /***********Save visitor user info end************/

        $response[] = array('app_package_name'=>$app_package_name,'app_name' => $app_name,'app_email' => $app_email,'app_logo' => $app_logo,'app_company' => $app_company,'app_website' => $app_website,'app_contact' => $app_contact,'facebook_link' => $facebook_link,'twitter_link' => $twitter_link,'instagram_link' => $instagram_link,'youtube_link' => $youtube_link,'google_play_link' => $google_play_link,'apple_store_link' => $apple_store_link,'app_version' => $app_version,'app_update_hide_show' => $app_update_hide_show,'app_update_version_code' => $app_update_version_code,'app_update_desc' => $app_update_desc,'app_update_link' => $app_update_link,'app_update_cancel_option' => $app_update_cancel_option,'song_download' => $song_download,'subscription_on_off' => $subscription_on_off,'ads_list'=>$ads_obj,'page_list'=>$pages_obj,'success'=>'1');

        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'user_status' => $user_status,
             'status_code' => 200
        )); 

    }

    public function payment_settings()
    {
        $get_data=checkSignSalt($_POST['data']);

        $settings = Settings::findOrFail('1'); 
        
        $gateway_list = PaymentGateway::where('status','1')->orderby('id')->get(); 

        $settings = Settings::findOrFail('1'); 

        $currency_code=$settings->currency_code;
        
        if(count($gateway_list))
        {
            foreach($gateway_list as $gateway_data)
            {
                    $gateway_id= $gateway_data->id;
                    $gateway_name= $gateway_data->gateway_name; 
                    $gateway_logo= \URL::to('/admin_assets/images/gateway/'.$gateway_data->id.'.png');
                    $gateway_info= json_decode($gateway_data->gateway_info);                  
                    $status= $gateway_data->status?"true":"false";              
                    
                    $response[]=array("gateway_id"=>$gateway_id,"gateway_name"=>$gateway_name,"gateway_logo"=>$gateway_logo,"gateway_info"=>$gateway_info,"status"=>$status);   
            }    
        }
        else
        {
            $response=array();    
        }
        

        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'currency_code' => $currency_code,
            'status_code' => 200,
            'success' => 1
        ));
 
    }
 
 
    public function home()
    {   

        $get_data=checkSignSalt($_POST['data']);
         
        $slider= Slider::where('status',1)->orderby('id','DESC')->get();

        foreach($slider as $slider_data)
        { 
            $response['slider'][]=array("slider_id"=>$slider_data->id,"slider_title"=>stripslashes($slider_data->slider_title),"slider_info"=>stripslashes($slider_data->slider_info),"songs_ids"=>$slider_data->songs_ids,"slider_image"=>\URL::to('/'.$slider_data->slider_image));
        }

       //Get Recently Songs
 
        if(isset($get_data['songs_ids']))
        {
            $songs_ids= explode(',',$get_data['songs_ids']);


            foreach($songs_ids as $song_id)
            { 
                    $song_obj_data= Songs::where('status',1)->where('id',$song_id)->first();
     
                    if($song_obj_data->song_type=="server_url")
                    {
                        $song_url= $song_obj_data->song_url;
                    }
                    else
                    {
                        $song_url= \URL::to('/'.$song_obj_data->song_url);
                    }     

                    $song_views= post_views_count($song_id);
                    $song_download= post_download_count($song_id);
                    $total_rate= PostRatings::getPostTotalRatings($song_id);
                    $favourite=check_favourite($song_id,isset($get_data['user_id'])?$get_data['user_id']:"");

                    $artist_ids= $song_obj_data->artist_ids;

                    if(!empty($artist_ids))
                    {
                        foreach(explode(',',$artist_ids) as $artist_id)
                        {
                             $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                             $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                        }
                    }
                    else
                    {
                        $artist_list=array();
                    }          
     
                    $response['recently_songs'][]=array("song_id"=>$song_obj_data->id,"song_title"=>stripslashes($song_obj_data->song_title),"song_image"=>\URL::to('/'.$song_obj_data->song_image),"song_info"=>stripslashes($song_obj_data->song_info),"song_lyrics"=>stripslashes($song_obj_data->song_lyrics),"song_type"=>$song_obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                    unset($artist_list);
            }
        }
        else
        {
            $response['recently_songs']=array();
        }
 
        //Trending Songs

        $trending_start_date = date('Y-m-d', strtotime('today - 30 days'));
        $trending_end_date = date('Y-m-d');

        $trending_now = PostViewsDownload::select("post_id","post_type")->whereBetween('date', array(strtotime($trending_start_date), strtotime($trending_end_date)))->selectRaw('SUM(post_views) as total_views')->groupBy('post_id','post_type')->orderby('total_views','DESC')->take(10)->get();

        if(count($trending_now) > 0)
        {
            foreach($trending_now as $obj_data)
            {   

                $song_title= Songs::getSongInfo($obj_data->post_id,'song_title');
                $song_image= \URL::to('/'.Songs::getSongInfo($obj_data->post_id,'song_image'));
                $song_info= Songs::getSongInfo($obj_data->post_id,'song_info');
                $song_lyrics= Songs::getSongInfo($obj_data->post_id,'song_lyrics');
                $song_type= Songs::getSongInfo($obj_data->post_id,'song_type');

                if($song_type=="server_url")
                {
                    $song_url= Songs::getSongInfo($obj_data->post_id,'song_url');
                }
                else
                {
                    $song_url= \URL::to('/'.Songs::getSongInfo($obj_data->post_id,'song_url'));
                }
                
                $song_views= post_views_count($obj_data->post_id);
                $song_download= post_download_count($obj_data->post_id);
                $total_rate= PostRatings::getPostTotalRatings($obj_data->post_id);
                $favourite=check_favourite($obj_data->post_id,isset($get_data['user_id'])?$get_data['user_id']:"");

                $artist_ids= Songs::getSongInfo($obj_data->post_id,'artist_ids');

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                        $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                        $artist_list1[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list1=array();
                }

                $response['trending_songs'][]=array("song_id"=>$obj_data->post_id,"song_title"=>$song_title,"song_image"=>$song_image,"song_info"=>$song_info,"song_lyrics"=>$song_lyrics,"song_type"=>$song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list1);

                unset($artist_list1);
            }
        }
        else
        {
            $response['trending_songs'] = array();
        }
        

         
        $home_sections = HomeSections::where('status',1)->orderby('id')->get();
        

        foreach($home_sections as $sections_data)
        {   
            if($sections_data->section_type=="category")
            {
                foreach(explode(",",$sections_data->post_ids) as $cat_data)
                {
                    $post_id=$cat_data;
                    $post_type="category";
                    $post_title= Category::getCategoryInfo($cat_data,'category_name');
                    $post_image=\URL::to('/'.Category::getCategoryInfo($cat_data,'category_image'));

                $home_content1[]= array("post_id"=>$post_id,"post_type"=>$post_type,"post_title"=>stripslashes($post_title),"post_image"=>$post_image);       
 
                }

                $response['home_sections'][]=array("home_id"=>$sections_data->id,"home_title"=>$sections_data->section_name,"home_type"=>$sections_data->section_type,"home_content"=>$home_content1);

                unset($home_content1);
            }

            if($sections_data->section_type=="artist")
            {
                foreach(explode(",",$sections_data->post_ids) as $artist_data)
                {
                    $post_id=$artist_data;
                    $post_type="artist";
                    $post_title= Artists::getArtistsInfo($artist_data,'artist_name');
                    $post_image=\URL::to('/'.Artists::getArtistsInfo($artist_data,'artist_image'));

                $home_content2[]= array("post_id"=>$post_id,"post_type"=>$post_type,"post_title"=>stripslashes($post_title),"post_image"=>$post_image);       
 
                }

                $response['home_sections'][]=array("home_id"=>$sections_data->id,"home_title"=>$sections_data->section_name,"home_type"=>$sections_data->section_type,"home_content"=>$home_content2);

                unset($home_content2);
            }

            if($sections_data->section_type=="album")
            {
                foreach(explode(",",$sections_data->post_ids) as $album_data)
                {
                    $post_id=$album_data;
                    $post_type="album";
                    $post_title= Album::getAlbumInfo($album_data,'album_name');
                    $post_image=\URL::to('/'.Album::getAlbumInfo($album_data,'album_image'));

                    $total_artist=total_album_artist($post_id);
                    $total_songs= Songs::where('status',1)->where('album_id',$post_id)->count();

                    $home_content3[]= array("post_id"=>$post_id,"post_type"=>$post_type,"post_title"=>stripslashes($post_title),"post_image"=>$post_image,"total_artist"=>$total_artist,"total_songs"=>$total_songs);       
 
                }

                $response['home_sections'][]=array("home_id"=>$sections_data->id,"home_title"=>$sections_data->section_name,"home_type"=>$sections_data->section_type,"home_content"=>$home_content3);

                unset($home_content3);
            }

            if($sections_data->section_type=="song")
            {
                foreach(explode(",",$sections_data->post_ids) as $song_data)
                {
                    $post_id=$song_data;
                    $post_type="song";
                    $post_title= Songs::getSongInfo($song_data,'song_title');
                    $post_image= \URL::to('/'.Songs::getSongInfo($song_data,'song_image'));
                    $song_info= Songs::getSongInfo($song_data,'song_info');
                    $song_lyrics= Songs::getSongInfo($song_data,'song_lyrics');

                    $song_type= Songs::getSongInfo($song_data,'song_type');

                    if($song_type=="server_url")
                    {
                        $song_url= Songs::getSongInfo($song_data,'song_url');
                    }
                    else
                    {
                        $song_url= \URL::to('/'.Songs::getSongInfo($song_data,'song_url'));
                    }   

                    $song_views= post_views_count($post_id);
                    $song_download= post_download_count($post_id);
                    $total_rate= PostRatings::getPostTotalRatings($post_id);  
                    $favourite=check_favourite($post_id,isset($get_data['user_id'])?$get_data['user_id']:"");

                    $artist_ids= Songs::getSongInfo($song_data,'artist_ids');

                    if(!empty($artist_ids))
                    {
                        foreach(explode(',',$artist_ids) as $artist_id)
                        {
                             $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                             $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                        }
                    }
                    else
                    {
                        $artist_list=array();
                    }           
  
                $home_content4[]= array("song_id"=>$post_id,"song_title"=>stripslashes($post_title),"song_image"=>$post_image,"song_info"=>stripslashes($song_info),"song_lyrics"=>stripslashes($song_lyrics),"song_type"=>$song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);    

                unset($artist_list);   
 
                }

                $response['home_sections'][]=array("home_id"=>$sections_data->id,"home_title"=>$sections_data->section_name,"home_type"=>$sections_data->section_type,"home_content"=>$home_content4);

                unset($home_content4);
            }

            if($sections_data->section_type=="playlist")
            {
                foreach(explode(",",$sections_data->post_ids) as $playlist_data)
                {
                    $post_id=$playlist_data;
                    $post_type="playlist";
                    $post_title= Playlist::getPlaylistInfo($playlist_data,'playlist_name');
                    $post_image=\URL::to('/'.Playlist::getPlaylistInfo($playlist_data,'playlist_image'));

                    $total_songs= total_playlist_songs($post_id);

                $home_content5[]= array("post_id"=>$post_id,"post_type"=>$post_type,"post_title"=>stripslashes($post_title),"post_image"=>$post_image,"total_songs"=>$total_songs);       
 
                }

                $response['home_sections'][]=array("home_id"=>$sections_data->id,"home_title"=>$sections_data->section_name,"home_type"=>$sections_data->section_type,"home_content"=>$home_content5);

                unset($home_content5);
            }  
 
        }

 
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
    }

    public function home_collections()
    {
        $get_data=checkSignSalt($_POST['data']);

        $id = $get_data['id'];

        $sections_data = HomeSections::where('id',$id)->where('status',1)->first();

           if($sections_data->section_type=="category")
            {
                foreach(explode(",",$sections_data->post_ids) as $cat_data)
                {
                    $post_id=$cat_data;
                    $post_type="category";
                    $post_title= Category::getCategoryInfo($cat_data,'category_name');
                    $post_image=\URL::to('/'.Category::getCategoryInfo($cat_data,'category_image'));

                $home_content1[]= array("category_id"=>$post_id,"type"=>$post_type,"category_name"=>stripslashes($post_title),"category_image"=>$post_image);       
 
                }

                $response=$home_content1;

                unset($home_content1);
            }

            if($sections_data->section_type=="artist")
            {
                foreach(explode(",",$sections_data->post_ids) as $artist_data)
                {
                    $post_id=$artist_data;
                    $post_type="artist";
                    $post_title= Artists::getArtistsInfo($artist_data,'artist_name');
                    $post_image=\URL::to('/'.Artists::getArtistsInfo($artist_data,'artist_image'));

                $home_content2[]= array("artist_id"=>$post_id,"type"=>$post_type,"artist_name"=>stripslashes($post_title),"artist_image"=>$post_image);       
 
                }

                $response=$home_content2;

                unset($home_content2);
            }

            if($sections_data->section_type=="album")
            {
                foreach(explode(",",$sections_data->post_ids) as $album_data)
                {
                    $post_id=$album_data;
                    $post_type="album";
                    $post_title= Album::getAlbumInfo($album_data,'album_name');
                    $post_image=\URL::to('/'.Album::getAlbumInfo($album_data,'album_image'));

                    $total_artist=total_album_artist($post_id);
                    $total_songs= Songs::where('status',1)->where('album_id',$post_id)->count();

                $home_content3[]= array("album_id"=>$post_id,"type"=>$post_type,"album_name"=>stripslashes($post_title),"album_image"=>$post_image,"total_artist"=>$total_artist,"total_songs"=>$total_songs);       
 
                }

                $response=$home_content3;

                unset($home_content3);
            }

            if($sections_data->section_type=="song")
            {
                foreach(explode(",",$sections_data->post_ids) as $song_data)
                {
                    $post_id=$song_data;
                    $post_type="song";
                    $post_title= Songs::getSongInfo($song_data,'song_title');
                    $post_image= \URL::to('/'.Songs::getSongInfo($song_data,'song_image'));
                    $song_info= Songs::getSongInfo($song_data,'song_info');
                    $song_lyrics= Songs::getSongInfo($song_data,'song_lyrics');

                    $song_type= Songs::getSongInfo($song_data,'song_type');

                    if($song_type=="server_url")
                    {
                        $song_url= Songs::getSongInfo($song_data,'song_url');
                    }
                    else
                    {
                        $song_url= \URL::to('/'.Songs::getSongInfo($song_data,'song_url'));
                    }            

                    $song_views= post_views_count($post_id);
                    $song_download= post_download_count($post_id);
                    $total_rate= PostRatings::getPostTotalRatings($post_id);
                    $favourite=check_favourite($post_id,isset($get_data['user_id'])?$get_data['user_id']:"");

                    $artist_ids= Songs::getSongInfo($song_data,'artist_ids');

                    if(!empty($artist_ids))
                    {
                        foreach(explode(',',$artist_ids) as $artist_id)
                        {
                             $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                             $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                        }
                    }
                    else
                    {
                        $artist_list=array();
                    }
  
                $home_content4[]= array("song_id"=>$post_id,"song_title"=>stripslashes($post_title),"song_image"=>$post_image,"song_info"=>stripslashes($song_info),"song_lyrics"=>stripslashes($song_lyrics),"song_type"=>$song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);  

                    unset($artist_list);     
 
                }

                $response=$home_content4;

                unset($home_content4);
            }

            if($sections_data->section_type=="playlist")
            {
                foreach(explode(",",$sections_data->post_ids) as $playlist_data)
                {
                    $post_id=$playlist_data;
                    $post_type="playlist";
                    $post_title= Playlist::getPlaylistInfo($playlist_data,'playlist_name');
                    $post_image=\URL::to('/'.Playlist::getPlaylistInfo($playlist_data,'playlist_image'));

                    $total_songs= total_playlist_songs($post_id);

                $home_content5[]= array("playlist_id"=>$post_id,"type"=>$post_type,"playlist_name"=>stripslashes($post_title),"playlist_image"=>$post_image,"total_songs"=>$total_songs);       
 
                }

                $response=$home_content5;

                unset($home_content5);
            }


        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
    }

    public function home_slider_songs()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $slider_id = $get_data['slider_id'];

        $obj_data= Slider::where('id',$slider_id)->first();

        $songs_ids=$obj_data->songs_ids;

        $slider_title= stripslashes($obj_data->slider_title);
        $slider_image= \URL::to('/'.$obj_data->slider_image);

        $songs_ids= explode(',',$obj_data->songs_ids);
 
        //$response=array("slider_id"=>$slider_id,"slider_title"=>$slider_title,"slider_image"=>$slider_image);

        //Get Songs
        foreach($songs_ids as $song_id)
        { 
                $song_obj_data= Songs::where('status',1)->where('id',$song_id)->first();
 
                if($song_obj_data->song_type=="server_url")
                {
                    $song_url= $song_obj_data->song_url;
                }
                else
                {
                    $song_url= \URL::to('/'.$song_obj_data->song_url);
                } 

                $song_views= post_views_count($song_id);
                $song_download= post_download_count($song_id);
                $total_rate= PostRatings::getPostTotalRatings($song_id);
                $favourite=check_favourite($song_id,isset($get_data['user_id'])?$get_data['user_id']:"");  

                $artist_ids= $song_obj_data->artist_ids;

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                         $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                         $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list=array();
                }       
 
                $response[]=array("song_id"=>$song_obj_data->id,"song_title"=>stripslashes($song_obj_data->song_title),"song_image"=>\URL::to('/'.$song_obj_data->song_image),"song_info"=>stripslashes($song_obj_data->song_info),"song_lyrics"=>stripslashes($song_obj_data->song_lyrics),"song_type"=>$song_obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                unset($artist_list);
        } 

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }

    public function home_recently_songs()
    {   
        
        $get_data=checkSignSalt($_POST['data']);
          
        $songs_ids= explode(',',$get_data['songs_ids']);
 
        //Get Songs
        foreach($songs_ids as $song_id)
        { 
                $song_obj_data= Songs::where('status',1)->where('id',$song_id)->first();
 
                if($song_obj_data->song_type=="server_url")
                {
                    $song_url= $song_obj_data->song_url;
                }
                else
                {
                    $song_url= \URL::to('/'.$song_obj_data->song_url);
                }     

                $song_views= post_views_count($song_id);
                $song_download= post_download_count($song_id);
                $total_rate= PostRatings::getPostTotalRatings($song_id);
                $favourite=check_favourite($song_id,isset($get_data['user_id'])?$get_data['user_id']:"");

                $artist_ids= $song_obj_data->artist_ids;

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                         $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                         $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list=array();
                }          
 
                $response[]=array("song_id"=>$song_obj_data->id,"song_title"=>stripslashes($song_obj_data->song_title),"song_image"=>\URL::to('/'.$song_obj_data->song_image),"song_info"=>stripslashes($song_obj_data->song_info),"song_lyrics"=>stripslashes($song_obj_data->song_lyrics),"song_type"=>$song_obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                unset($artist_list);
        }
         

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }

    public function category()
    {    

        $get_data=checkSignSalt($_POST['data']);

        $data_list= Category::where('status',1)->orderby('id','DESC')->paginate($this->pagination_limit);

        foreach($data_list as $obj_data)
        { 
            $response[]=array("category_id"=>$obj_data->id,"category_name"=>stripslashes($obj_data->category_name),"category_image"=>\URL::to('/'.$obj_data->category_image));
        }

        $total_records=Category::where('status',1)->count();

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function song_by_category()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $cat_id = $get_data['category_id'];

        $total_records=Songs::where('status',1)->where('cat_id',$cat_id)->orderby('id','DESC')->count();


        $data_list= Songs::where('status',1)->where('cat_id',$cat_id)->orderby('id','DESC')->paginate($this->pagination_limit);
        
        if($total_records > 0)
        {
            foreach($data_list as $obj_data)
            { 
                if($obj_data->song_type=="server_url")
                {
                    $song_url= $obj_data->song_url;
                }
                else
                {
                    $song_url= \URL::to('/'.$obj_data->song_url);
                }
                
                $song_views= post_views_count($obj_data->id);
                $song_download= post_download_count($obj_data->id);
                $total_rate= PostRatings::getPostTotalRatings($obj_data->id);
                $favourite=check_favourite($obj_data->id,isset($get_data['user_id'])?$get_data['user_id']:"");

                $artist_ids= $obj_data->artist_ids;

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                         $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                         $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list=array();
                }

                $response[]=array("song_id"=>$obj_data->id,"song_title"=>stripslashes($obj_data->song_title),"song_image"=>\URL::to('/'.$obj_data->song_image),"song_info"=>stripslashes($obj_data->song_info),"song_lyrics"=>stripslashes($obj_data->song_lyrics),"song_type"=>$obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                unset($artist_list);
            }
        }
        else
        {
            $response=array();
        }


         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function artists()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $data_list= Artists::orderby('id','DESC')->paginate($this->pagination_limit);

        foreach($data_list as $obj_data)
        { 
            $response[]=array("artist_id"=>$obj_data->id,"artist_name"=>stripslashes($obj_data->artist_name),"artist_image"=>\URL::to('/'.$obj_data->artist_image));
        }

        $total_records=Artists::orderby('id','DESC')->count();

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function song_by_artists()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $artist_id = $get_data['artist_id'];

        $data_list= Songs::where('status',1)->whereRaw("find_in_set('$artist_id',artist_ids)")->orderby('id','DESC')->paginate($this->pagination_limit);

        $total_records=Songs::where('status',1)->whereRaw("find_in_set('$artist_id',artist_ids)")->count();

       if($total_records > 0)
       { 

            foreach($data_list as $obj_data)
            { 
                if($obj_data->song_type=="server_url")
                {
                    $song_url= $obj_data->song_url;
                }
                else
                {
                    $song_url= \URL::to('/'.$obj_data->song_url);
                }
                
                $song_views= post_views_count($obj_data->id);
                $song_download= post_download_count($obj_data->id);
                $total_rate= PostRatings::getPostTotalRatings($obj_data->id);
                $favourite=check_favourite($obj_data->id,isset($get_data['user_id'])?$get_data['user_id']:"");

                $artist_ids= $obj_data->artist_ids;

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                         $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                         $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list=array();
                }

                $response[]=array("song_id"=>$obj_data->id,"song_title"=>stripslashes($obj_data->song_title),"song_image"=>\URL::to('/'.$obj_data->song_image),"song_info"=>stripslashes($obj_data->song_info),"song_lyrics"=>stripslashes($obj_data->song_lyrics),"song_type"=>$obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                unset($artist_list);
            }
        }
        else
        {
            $response=array();
        }

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function album_by_artists()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $artist_id = $get_data['artist_id'];

        $data_list= Album::where('status',1)->whereRaw("find_in_set('$artist_id',artist_ids)")->orderby('id','DESC')->paginate($this->pagination_limit);

        foreach($data_list as $obj_data)
        { 
             $response[]=array("album_id"=>$obj_data->id,"album_name"=>stripslashes($obj_data->album_name),"album_image"=>\URL::to('/'.$obj_data->album_image));
        }

        $total_records=Album::where('status',1)->whereRaw("find_in_set('$artist_id',artist_ids)")->count();

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function album()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $data_list= Album::where('status',1)->orderby('id','DESC')->paginate($this->pagination_limit);

        foreach($data_list as $obj_data)
        { 
            $total_artist=total_album_artist($obj_data->id);
            $total_songs= Songs::where('status',1)->where('album_id',$obj_data->id)->count();

            $response[]=array("album_id"=>$obj_data->id,"album_name"=>stripslashes($obj_data->album_name),"album_image"=>\URL::to('/'.$obj_data->album_image),"total_artist"=>$total_artist,"total_songs"=>$total_songs);
        }

        $total_records=Album::where('status',1)->count();

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function song_by_album()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $album_id = $get_data['album_id'];

        $data_list= Songs::where('status',1)->where('album_id',$album_id)->orderby('id','DESC')->paginate($this->pagination_limit);

        $total_records=Songs::where('status',1)->where('album_id',$album_id)->count();

        if($total_records > 0)
        {

            foreach($data_list as $obj_data)
            { 
                if($obj_data->song_type=="server_url")
                {
                    $song_url= $obj_data->song_url;
                }
                else
                {
                    $song_url= \URL::to('/'.$obj_data->song_url);
                }
                
                $song_views= post_views_count($obj_data->id);
                $song_download= post_download_count($obj_data->id);
                $total_rate= PostRatings::getPostTotalRatings($obj_data->id);
                $favourite=check_favourite($obj_data->id,isset($get_data['user_id'])?$get_data['user_id']:"");

                $artist_ids= $obj_data->artist_ids;

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                         $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                         $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list=array();
                }  

                $response[]=array("song_id"=>$obj_data->id,"song_title"=>stripslashes($obj_data->song_title),"song_image"=>\URL::to('/'.$obj_data->song_image),"song_info"=>stripslashes($obj_data->song_info),"song_lyrics"=>stripslashes($obj_data->song_lyrics),"song_type"=>$obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                unset($artist_list);
            }
        }
        else
        {
            $response=array();
        }

         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function playlist()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $data_list= Playlist::where('status',1)->orderby('id','DESC')->paginate($this->pagination_limit);

        foreach($data_list as $obj_data)
        { 
            $total_songs= total_playlist_songs($obj_data->id);

            $response[]=array("playlist_id"=>$obj_data->id,"playlist_name"=>stripslashes($obj_data->playlist_name),"playlist_image"=>\URL::to('/'.$obj_data->playlist_image),"total_songs"=>$total_songs);
        }

        $total_records=Playlist::where('status',1)->count();

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function song_by_playlist()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $playlist_id = $get_data['playlist_id'];
         
        $obj_data= Playlist::where('id',$playlist_id)->first();

        $playlist_name= stripslashes($obj_data->playlist_name);
        $playlist_image= \URL::to('/'.$obj_data->playlist_image);

        $songs_ids= explode(',',$obj_data->songs_ids);
 
        //Get Songs
        foreach($songs_ids as $song_id)
        { 
                $song_obj_data= Songs::where('status',1)->where('id',$song_id)->first();
 
                if($song_obj_data->song_type=="server_url")
                {
                    $song_url= $song_obj_data->song_url;
                }
                else
                {
                    $song_url= \URL::to('/'.$song_obj_data->song_url);
                }      

                $song_views= post_views_count($song_id);
                $song_download= post_download_count($song_id);
                $total_rate= PostRatings::getPostTotalRatings($song_id);
                $favourite=check_favourite($song_id,isset($get_data['user_id'])?$get_data['user_id']:"");

                $artist_ids= $song_obj_data->artist_ids;

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                         $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                         $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list=array();
                }       
 
                $response[]=array("song_id"=>$song_obj_data->id,"song_title"=>stripslashes($song_obj_data->song_title),"song_image"=>\URL::to('/'.$song_obj_data->song_image),"song_info"=>stripslashes($song_obj_data->song_info),"song_lyrics"=>stripslashes($song_obj_data->song_lyrics),"song_type"=>$song_obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                unset($artist_list);
        }         

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }


    public function song_details()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $song_id = $get_data['song_id'];

        $obj_data= Songs::where('status',1)->where('id',$song_id)->first();

        
        if($obj_data->song_type=="server_url")
        {
            $song_url= $obj_data->song_url;
        }
        else
        {
            $song_url= \URL::to('/'.$obj_data->song_url);
        }
         
        $song_views= post_views_count($song_id);
        $song_download= post_download_count($song_id);
        $total_rate= PostRatings::getPostTotalRatings($song_id);

        $user_id= isset($get_data['user_id'])?$get_data['user_id']:"";

        $favourite= check_favourite($song_id,$user_id);

        $user_rate= PostRatings::getUserRatings($song_id,$user_id);

        $artist_ids= $obj_data->artist_ids;

        if(!empty($artist_ids))
        {
            foreach(explode(',',$artist_ids) as $artist_id)
            {
                 $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                 $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
            }
        }
        else
        {
            $artist_list=array();
        }


        $response[]=array("song_id"=>$obj_data->id,"song_title"=>stripslashes($obj_data->song_title),"song_image"=>\URL::to('/'.$obj_data->song_image),"song_info"=>stripslashes($obj_data->song_info),"song_lyrics"=>stripslashes($obj_data->song_lyrics),"song_type"=>$obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"user_rate"=>$user_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);
         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }

    public function latest_songs()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $data_list= Songs::where('status',1)->orderby('id','DESC')->paginate($this->pagination_limit);

        foreach($data_list as $obj_data)
        { 
            if($obj_data->song_type=="server_url")
            {
                $song_url= $obj_data->song_url;
            }
            else
            {
                $song_url= \URL::to('/'.$obj_data->song_url);
            }
            
            $song_views= post_views_count($obj_data->id);
            $song_download= post_download_count($obj_data->id);
            $total_rate= PostRatings::getPostTotalRatings($obj_data->id);
            $favourite=check_favourite($obj_data->id,isset($get_data['user_id'])?$get_data['user_id']:"");

            $artist_ids= $obj_data->artist_ids;

            if(!empty($artist_ids))
            {
                foreach(explode(',',$artist_ids) as $artist_id)
                {
                     $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                     $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                }
            }
            else
            {
                $artist_list=array();
            }

            $response[]=array("song_id"=>$obj_data->id,"song_title"=>stripslashes($obj_data->song_title),"song_image"=>\URL::to('/'.$obj_data->song_image),"song_info"=>stripslashes($obj_data->song_info),"song_lyrics"=>stripslashes($obj_data->song_lyrics),"song_type"=>$obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

            unset($artist_list);
        }

        $total_records=Songs::where('status',1)->count();


         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function trending_songs()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $trending_start_date = date('Y-m-d', strtotime('today - 30 days'));
        $trending_end_date = date('Y-m-d');

        $trending_now = PostViewsDownload::select("post_id","post_type")->whereBetween('date', array(strtotime($trending_start_date), strtotime($trending_end_date)))->selectRaw('SUM(post_views) as total_views')->groupBy('post_id','post_type')->orderby('total_views','DESC')->take(50)->get();


        foreach($trending_now as $obj_data)
        {   

            $song_title= Songs::getSongInfo($obj_data->post_id,'song_title');
            $song_image= \URL::to('/'.Songs::getSongInfo($obj_data->post_id,'song_image'));
            $song_info= Songs::getSongInfo($obj_data->post_id,'song_info');
            $song_lyrics= Songs::getSongInfo($obj_data->post_id,'song_lyrics');
            $song_type= Songs::getSongInfo($obj_data->post_id,'song_type');

            if($song_type=="server_url")
            {
                $song_url= Songs::getSongInfo($obj_data->post_id,'song_url');
            }
            else
            {
                $song_url= \URL::to('/'.Songs::getSongInfo($obj_data->post_id,'song_url'));
            }
            
            $song_views= post_views_count($obj_data->post_id);
            $song_download= post_download_count($obj_data->post_id);
            $total_rate= PostRatings::getPostTotalRatings($obj_data->post_id);
            $favourite=check_favourite($obj_data->post_id,isset($get_data['user_id'])?$get_data['user_id']:"");

            $artist_ids= Songs::getSongInfo($obj_data->post_id,'artist_ids');

            if(!empty($artist_ids))
            {
                foreach(explode(',',$artist_ids) as $artist_id)
                {
                     $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                     $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                }
            }
            else
            {
                $artist_list=array();
            }

            $response[]=array("song_id"=>$obj_data->post_id,"song_title"=>$song_title,"song_image"=>$song_image,"song_info"=>$song_info,"song_lyrics"=>$song_lyrics,"song_type"=>$song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

            unset($artist_list);
        }
 

         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
             'status_code' => 200
        ));

    }

    public function song_view()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $post_id = $get_data['post_id'];

          
        //View Update
        post_views_save($post_id,"Song");

        $song_views= post_views_count($post_id);

        $response[]=array("views"=>$song_views);
         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }

    public function song_download()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $post_id = $get_data['post_id'];

          
        //Download Update
        post_download_save($post_id,"Song");

        $song_download= post_download_count($post_id);

        $response[]=array("download"=>$song_download);
         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }

    public function song_rate()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $user_id = $get_data['user_id'];
        $post_id = $get_data['post_id'];
        $rate = $get_data['rate'];

        $rate_info = PostRatings::where('post_id', '=', $post_id)->where('user_id', '=', $user_id)->first();   


        if($rate_info)
        { 
            $rate_obj = PostRatings::findOrFail($rate_info->id);        
            $rate_obj->rate = $rate;
            $rate_obj->save();

            $msg=trans('words.rate_updated');
             
        }
        else
        {
            $rate_obj = new PostRatings;

            $rate_obj->post_id = $post_id;
            $rate_obj->user_id = $user_id;
            $rate_obj->rate = $rate;
            $rate_obj->save();

            $msg=trans('words.rate_success');
        }  
         

        $total_rate= PostRatings::getPostTotalRatings($post_id);

        $response[]=array("total_rate"=>$total_rate,"msg"=>$msg);
         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }

    public function song_favourite()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $user_id = $get_data['user_id'];
        $post_id = $get_data['post_id'];
 
        $fav_info = Favourite::where('post_id', '=', $post_id)->where('user_id', '=', $user_id)->first();   


        if($fav_info)
        { 
            $fav_obj = Favourite::findOrFail($fav_info->id);        
            $fav_obj->delete();

            $success=false;
            $msg=trans('words.fav_deleted');
             
        }
        else
        {
            $fav_obj = new Favourite;

            $fav_obj->post_id = $post_id;
            $fav_obj->user_id = $user_id;
            $fav_obj->save();

            $success=true;
            $msg=trans('words.fav_success');
        }  
          
        $response[]=array("success"=>$success,"msg"=>$msg);
         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));

    }

    public function search()
    {

        $get_data=checkSignSalt($_POST['data']);


        $search_text = $get_data['search_text'];

        //Category
        $cat_list= Category::where('status',1)->where("category_name", "LIKE","%$search_text%")->orderby('category_name')->take(10)->get();

        foreach($cat_list as $obj_data)
        { 
            $response['category_list'][]=array("category_id"=>$obj_data->id,"category_name"=>stripslashes($obj_data->category_name),"category_image"=>\URL::to('/'.$obj_data->category_image));
        }

        //Artists
        $artis_list= Artists::where("artist_name", "LIKE","%$search_text%")->orderby('artist_name')->take(10)->get();

        foreach($artis_list as $obj_data)
        { 
            $response['artist_list'][]=array("artist_id"=>$obj_data->id,"artist_name"=>stripslashes($obj_data->artist_name),"artist_image"=>\URL::to('/'.$obj_data->artist_image));
        }

        //Album
        $album_list= Album::where('status',1)->where("album_name", "LIKE","%$search_text%")->orderby('album_name')->take(10)->get();

        foreach($album_list as $obj_data)
        { 
            $total_artist=total_album_artist($obj_data->id);
            $total_songs= Songs::where('status',1)->where('album_id',$obj_data->id)->count();

            $response['album_list'][]=array("album_id"=>$obj_data->id,"album_name"=>stripslashes($obj_data->album_name),"album_image"=>\URL::to('/'.$obj_data->album_image),"total_artist"=>$total_artist,"total_songs"=>$total_songs);
        }

        //Playlist
        $playlist_list= Playlist::where('status',1)->where("playlist_name", "LIKE","%$search_text%")->orderby('playlist_name')->take(10)->get();

        foreach($playlist_list as $obj_data)
        { 
            $total_songs= total_playlist_songs($obj_data->id);

            $response['playlist_list'][]=array("playlist_id"=>$obj_data->id,"playlist_name"=>stripslashes($obj_data->playlist_name),"playlist_image"=>\URL::to('/'.$obj_data->playlist_image),"total_songs"=>$total_songs);
        }

        //Songs
        $data_list= Songs::where('status',1)->where("song_title", "LIKE","%$search_text%")->orderby('song_title')->take(10)->get();

        foreach($data_list as $obj_data)
        { 
            if($obj_data->song_type=="server_url")
            {
                $song_url= $obj_data->song_url;
            }
            else
            {
                $song_url= \URL::to('/'.$obj_data->song_url);
            }
            
            $song_views= post_views_count($obj_data->id);
            $song_download= post_download_count($obj_data->id);
            $total_rate= PostRatings::getPostTotalRatings($obj_data->id);
            $favourite=check_favourite($obj_data->id,isset($get_data['user_id'])?$get_data['user_id']:"");

            $artist_ids= $obj_data->artist_ids;

            if(!empty($artist_ids))
            {
                foreach(explode(',',$artist_ids) as $artist_id)
                {
                     $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                     $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                }
            }
            else
            {
                $artist_list=array();
            }

            $response['songs_list'][]=array("song_id"=>$obj_data->id,"song_title"=>stripslashes($obj_data->song_title),"song_image"=>\URL::to('/'.$obj_data->song_image),"song_info"=>stripslashes($obj_data->song_info),"song_lyrics"=>stripslashes($obj_data->song_lyrics),"song_type"=>$obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

            unset($artist_list);
        }

        return \Response::json(array(            
                'ONLINE_MP3_APP' => $response,
                 'status_code' => 200
            ));
    }

    public function search_single()
    {
        $get_data=checkSignSalt($_POST['data']);


        $search_text = $get_data['search_text'];
        $search_type = $get_data['search_type'];

        if($search_type=="category")
        {
            //Category
            $cat_list= Category::where('status',1)->where("category_name", "LIKE","%$search_text%")->orderby('category_name')->paginate($this->pagination_limit);

            $total_records=Category::where('status',1)->where("category_name", "LIKE","%$search_text%")->count();

            if($total_records > 0)
            {
                foreach($cat_list as $obj_data)
                { 
                    $response[]=array("category_id"=>$obj_data->id,"category_name"=>stripslashes($obj_data->category_name),"category_image"=>\URL::to('/'.$obj_data->category_image));
                }
            }
            else
            {
                 $response=array();
            }

            
        }
        else if($search_type=="artist")
        {
            //Artists
            $artis_list= Artists::where("artist_name", "LIKE","%$search_text%")->orderby('artist_name')->paginate($this->pagination_limit);
            
            $total_records=Artists::where("artist_name", "LIKE","%$search_text%")->count();

            if($total_records > 0)
            {
                foreach($artis_list as $obj_data)
                { 
                    $response[]=array("artist_id"=>$obj_data->id,"artist_name"=>stripslashes($obj_data->artist_name),"artist_image"=>\URL::to('/'.$obj_data->artist_image));
                }
            }
            else
            {
                $response=array();
            }
        }
        else if($search_type=="album")
        {
            //Album
            $album_list= Album::where('status',1)->where("album_name", "LIKE","%$search_text%")->orderby('album_name')->paginate($this->pagination_limit);

            $total_records=Album::where('status',1)->where("album_name", "LIKE","%$search_text%")->count();

            if($total_records > 0)
            {
                foreach($album_list as $obj_data)
                { 
                    $total_artist=total_album_artist($obj_data->id);
                    $total_songs= Songs::where('status',1)->where('album_id',$obj_data->id)->count();

                    $response[]=array("album_id"=>$obj_data->id,"album_name"=>stripslashes($obj_data->album_name),"album_image"=>\URL::to('/'.$obj_data->album_image),"total_artist"=>$total_artist,"total_songs"=>$total_songs);
                }
            }
            else
            {
                $response=array();
            }    
        }
        else if($search_type=="playlist")
        {
            //Playlist
            $playlist_list= Playlist::where('status',1)->where("playlist_name", "LIKE","%$search_text%")->orderby('playlist_name')->paginate($this->pagination_limit);
           
            $total_records=Playlist::where('status',1)->where("playlist_name", "LIKE","%$search_text%")->count();

            if($total_records > 0)
            {
                foreach($playlist_list as $obj_data)
                {   
                    $total_songs= total_playlist_songs($obj_data->id);

                    $response[]=array("playlist_id"=>$obj_data->id,"playlist_name"=>stripslashes($obj_data->playlist_name),"playlist_image"=>\URL::to('/'.$obj_data->playlist_image),"total_songs"=>$total_songs);
                }
            }
            else
            {
                $response=array();
            }
        }
        else
        {
            //Songs
            $data_list= Songs::where('status',1)->where("song_title", "LIKE","%$search_text%")->orderby('song_title')->paginate($this->pagination_limit);

            $total_records=Songs::where('status',1)->where("song_title", "LIKE","%$search_text%")->count();

            if($total_records)
            {
                foreach($data_list as $obj_data)
                { 
                    if($obj_data->song_type=="server_url")
                    {
                        $song_url= $obj_data->song_url;
                    }
                    else
                    {
                        $song_url= \URL::to('/'.$obj_data->song_url);
                    }
                    
                    $song_views= post_views_count($obj_data->id);
                    $song_download= post_download_count($obj_data->id);
                    $total_rate= PostRatings::getPostTotalRatings($obj_data->id);
                    $favourite=check_favourite($obj_data->id,isset($get_data['user_id'])?$get_data['user_id']:"");

                    $artist_ids= $obj_data->artist_ids;

                    if(!empty($artist_ids))
                    {
                        foreach(explode(',',$artist_ids) as $artist_id)
                        {
                             $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                             $artist_list[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                        }
                    }
                    else
                    {
                        $artist_list=array();
                    }

                    $response[]=array("song_id"=>$obj_data->id,"song_title"=>stripslashes($obj_data->song_title),"song_image"=>\URL::to('/'.$obj_data->song_image),"song_info"=>stripslashes($obj_data->song_info),"song_lyrics"=>stripslashes($obj_data->song_lyrics),"song_type"=>$obj_data->song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list);

                    unset($artist_list);
                }
            } 
            else
            {
                $response=array();
            }
            

        }

        return \Response::json(array(            
                'ONLINE_MP3_APP' => $response,
                'total_records' => $total_records,
                'status_code' => 200
            ));

    }

      
    public function login()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

          
        $email=isset($get_data['email'])?$get_data['email']:'';
        $password=isset($get_data['password'])?$get_data['password']:'';
        
        if ($email=='' AND $password=='')
        {
                 
               $response[] = array('msg' => trans('words.email_pass_req'),'success'=>'0'); 

                return \Response::json(array(            
                    'ONLINE_MP3_APP' => $response,
                    'status_code' => 200
                ));
        }
 
        $user_info = User::where('email',$email)->first(); 

         
        if (Hash::check($password, $user_info['password'])) 
        {
           
            if($user_info->status==0){
                  
                  $response[] = array('msg' => trans('words.account_banned'),'success'=>'0');
            }             
            else
            { 
                $user_id=$user_info->id;
                $user = User::findOrFail($user_id);

                \Auth::attempt(['email' => $user->email, 'password' => null]);

                $brand=isset($get_data['brand'])?$get_data['brand']:'';
                $model=isset($get_data['model'])?$get_data['model']:'';
                $platform=isset($get_data['platform'])?$get_data['platform']:'';

                $user_device_name= $brand.' '.$model.' '.$platform;

                 //Save History
                $user_session_name=Session::getId();

                $ck_user_device_obj = UsersDeviceHistory::where('user_id',$user_id)->where('user_session_name',$user_session_name)->first();

                if($ck_user_device_obj)
                {
                    $device_h_id=$ck_user_device_obj->id;

                    $user_device_obj = UsersDeviceHistory::findOrFail($device_h_id);

                    $user_device_obj->user_id = $user_id;
                    $user_device_obj->user_device_name=$user_device_name;   
                    $user_device_obj->user_session_name=Session::getId();
                    $user_device_obj->date_time= strtotime(date('m/d/Y h:i:s'));   
                    $user_device_obj->save();
                }
                else
                {
                    $user_device_obj = new UsersDeviceHistory;

                    $user_device_obj->user_id = $user_id;
                    $user_device_obj->user_device_name=$user_device_name;   
                    $user_device_obj->user_session_name=Session::getId();   
                    $user_device_obj->date_time= strtotime(date('m/d/Y h:i:s'));
                    $user_device_obj->save();
                }

                /***Save Device End***/
                //Device limit check
                $plan_id=$user->plan_id;
                if(user_device_limit_reached($user_id,$plan_id))
                {                 
                    $device_limit_reached = true;
                }
                else
                {
                    $device_limit_reached = false;
                }
                 
                if($user->user_image!='')
                {
                    $user_image=\URL::asset('upload/'.$user->user_image);
                }
                else
                {
                    $user_image=\URL::asset('upload/profile.png');
                }
                $phone= $user->phone?$user->phone:'';

                $response[] = array('user_session_name' => $user_session_name,'device_limit_reached' => $device_limit_reached,'user_id' => $user_id,'name' => $user->name,'email' => $user->email,'phone' => $phone,'user_image' => $user_image,'msg' => trans('words.login_success'),'success'=>'1');
            }

        }
        else
        {
            $response[] = array('msg' => trans('words.email_password_invalid'),'success'=>'0');
        }
        
        
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));   
    }

    public function signup()
    { 
        $get_data=checkSignSalt($_POST['data']);
            
        $name= $get_data['name'];
        $email= $get_data['email'];
        $password= $get_data['password'];
        $phone= $get_data['phone'];
        
        $check_email = User::where('email', $email)->first();

        if($check_email)
        {
            $response[] = array('msg' => trans('words.email_already_used'),'success'=>'0'); 

                return \Response::json(array(            
                    'ONLINE_MP3_APP' => $response,
                    'status_code' => 200
                ));
        }
        else
        {   
            $user = new User;

            $user->usertype = 'User';
            $user->name = $name; 
            $user->email = $email;         
            $user->password= bcrypt($password);  
            $user->phone= $phone?$phone:'';        
            $user->save();

            $response[] = array('msg' => trans('words.account_created_successfully'),'success'=>'1');
            return \Response::json(array(            
                'ONLINE_MP3_APP' => $response,
                'status_code' => 200
            ));
        }
    }

    public function social_login()
    {   
        
        $get_data=checkSignSalt($_POST['data']);
            
        $login_type= $get_data['login_type']; // FB or Google
        $social_id= $get_data['social_id'];

        $name= $get_data['name'];
        $email= $get_data['email'];
        $password= bcrypt('123456dummy');
        $phone= '';
        
        $check_email = User::where('email', $email)->first();

        if($check_email)
        {
            $finduser = User::where('email', $email)->first();
        }
        else
        {
            if($login_type=="google")
            {
                 $finduser = User::where('google_id', $social_id)->first();
     
            }
            else
            {
                 $finduser = User::where('facebook_id', $social_id)->first();
      
            }
        }

        if($finduser)
        {
                if($finduser->user_image!='')
                {
                    $user_image=\URL::asset('upload/'.$finduser->user_image);
                }
                else
                {
                    $user_image=\URL::asset('upload/profile.png');
                }

                if($finduser->status==0){
                 
                  $response[] = array('msg' => trans('words.account_banned'),'success'=>'0');
                }
                else
                {

                    $user_id=$finduser->id;
                  
                    $user_login_ojb = User::where('id', $user_id)->first(); 
                    \Auth::login($user_login_ojb);

                    //\Auth::attempt(['email' => $finduser->email, 'password' => null]);

                    /***Save Device***/                     
                    $brand=isset($get_data['brand'])?$get_data['brand']:'';
                    $model=isset($get_data['model'])?$get_data['model']:'';
                    $platform=isset($get_data['platform'])?$get_data['platform']:'';

                    $user_device_name= $brand.' '.$model.' '.$platform;

                     //Save History
                    $user_session_name=Session::getId();

                    $ck_user_device_obj = UsersDeviceHistory::where('user_id',$user_id)->where('user_session_name',$user_session_name)->first();

                    if($ck_user_device_obj)
                    {
                        $device_h_id=$ck_user_device_obj->id;

                        $user_device_obj = UsersDeviceHistory::findOrFail($device_h_id);

                        $user_device_obj->user_id = $user_id;
                        $user_device_obj->user_device_name=$user_device_name;   
                        $user_device_obj->user_session_name=Session::getId();
                        $user_device_obj->date_time= strtotime(date('m/d/Y h:i:s'));   
                        $user_device_obj->save();
                    }
                    else
                    {
                        $user_device_obj = new UsersDeviceHistory;

                        $user_device_obj->user_id = $user_id;
                        $user_device_obj->user_device_name=$user_device_name;   
                        $user_device_obj->user_session_name=Session::getId();
                        $user_device_obj->date_time= strtotime(date('m/d/Y h:i:s'));   
                        $user_device_obj->save();
                    }

                    /***Save Device End***/
                    //Device limit check
                    $plan_id=$finduser->plan_id;
                    

                    if(user_device_limit_reached($user_id,$plan_id))
                    {   
                               
                        $device_limit_reached = true;
                    }
                    else
                    {
                        $device_limit_reached = false;
                    }   

                 $phone= $finduser->phone?$finduser->phone:'';       
                
                 $response[] = array('user_session_name' => $user_session_name,'device_limit_reached' => $device_limit_reached,'user_id' => $finduser->id,'name' => $finduser->name,'email' => $finduser->email,'phone' => $phone,'user_image' => $user_image,'msg' => trans('words.login_success'),'success'=>'1');
                }
        }
        else
        {

            
            $social_login_type="google";
            $google_id=$social_id;

            $user_obj = new User;

            $user_obj->usertype = 'User';
            $user_obj->social_login_type = $social_login_type; 
            $user_obj->google_id = $google_id; 
            $user_obj->name = $name; 
            $user_obj->email = $email;         
            $user_obj->password= bcrypt($password);  
            $user_obj->phone= $phone?$phone:'';        
            $user_obj->save();
     
             
            //Get last insert user id
            $user_id=$user_obj->id;
 
            $user = User::findOrFail($user_id);

                 
            if($user->user_image!='')
            {
                $user_image=\URL::asset('upload/'.$user->user_image);
            }
            else
            {
                $user_image=\URL::asset('upload/profile.png');
            }


            $user_login_ojb = User::where('id', $user_id)->first(); 
            \Auth::login($user_login_ojb);


            $brand=isset($get_data['brand'])?$get_data['brand']:'';
            $model=isset($get_data['model'])?$get_data['model']:'';
            $platform=isset($get_data['platform'])?$get_data['platform']:'';

            $user_device_name= $brand.' '.$model.' '.$platform;

             //Save History
            $user_session_name=Session::getId();

            $ck_user_device_obj = UsersDeviceHistory::where('user_id',$user_id)->where('user_session_name',$user_session_name)->first();

            if($ck_user_device_obj)
            {
                $device_h_id=$ck_user_device_obj->id;

                $user_device_obj = UsersDeviceHistory::findOrFail($device_h_id);

                $user_device_obj->user_id = $user_id;
                $user_device_obj->user_device_name=$user_device_name;   
                $user_device_obj->user_session_name=Session::getId();
                $user_device_obj->date_time= strtotime(date('m/d/Y h:i:s'));   
                $user_device_obj->save();
            }
            else
            {
                $user_device_obj = new UsersDeviceHistory;

                $user_device_obj->user_id = $user_id;
                $user_device_obj->user_device_name=$user_device_name;   
                $user_device_obj->user_session_name=Session::getId();
                $user_device_obj->date_time= strtotime(date('m/d/Y h:i:s'));   
                $user_device_obj->save();
            }

            /***Save Device End***/ 
            //Device limit check
            $plan_id=$user->plan_id;
            if(user_device_limit_reached($user_id,$plan_id))
            {                 
                $device_limit_reached = true;
            }
            else
            {
                $device_limit_reached = false;
            }


            $phone= $user->phone?$user->phone:'';

            $response[] = array('user_session_name' => $user_session_name,'device_limit_reached' => $device_limit_reached,'user_id' => $user_id,'name' => $user->name,'email' => $user->email,'phone' => $phone,'user_image' => $user_image,'msg' => trans('words.login_success'),'success'=>'1');
        }

 
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));   
    }
     
 
    public function forgot_password()
    {   
        $get_data=checkSignSalt($_POST['data']);

        $email=isset($get_data['email'])?$get_data['email']:'';
 
        $user = User::where('email', $email)->first();

        //dd($user);
        //exit;

        if(!$user)
        {
            $response[] = array('msg' => trans('words.email_not_found'),'success'=>'1');
        }
        else
        {  
           $user_id=$user->id;
           $name=$user->name;
           $email=$user->email;

           $password= Str::random(10);

           $user = User::findOrFail($user_id);
           $user->password= bcrypt($password);
           $user->save(); 
    
           try{

            $data_email = array(
                'name' => $name,
                'password' => $password
                );    

            \Mail::send('emails.password', $data_email, function($message) use ($name,$email){
                $message->to($email, $name)
                ->from(getcong('app_email'), getcong('app_name'))
                ->subject('Password Reset | '.getcong('app_name'));
            });    

            }catch (\Throwable $e) {
                     
                \Log::info($e->getMessage());    
            }     
     
           
           $response[] = array('msg' => trans('words.email_new_pass_sent'),'success'=>'1');
 
        }

        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
    }

    public function logout()
    {
        $get_data=checkSignSalt($_POST['data']);

        $user_id=$get_data['user_id'];
 
        //Delete session file
        $user_session_name=$get_data['user_session_name'];        
        \Session::getHandler()->destroy($user_session_name);
 
        $user_device_obj = UsersDeviceHistory::where('user_id',$user_id)->where('user_session_name',$user_session_name);
        $user_device_obj->delete();

        $response[] = array('msg' => trans('words.logout'),'success'=>'1');
        
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
    }
     
    public function profile()
    {   
        $get_data=checkSignSalt($_POST['data']);

        $user_id=$get_data['user_id'];

        $user = User::findOrFail($user_id);

                 
        if($user->user_image!='')
        {
            $user_image=\URL::asset('upload/'.$user->user_image);
        }
        else
        {
            $user_image=\URL::asset('upload/profile.jpg');
        }

        $phone=$user->phone?$user->phone:'';
 
        $response[] = array('user_id' => $user_id,'name' => $user->name,'email' => $user->email,'phone' => $phone,'user_image' => $user_image,'msg' => trans('words.profile'),'success'=>'1');


        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
    }

    public function profile_update(Request $request)
    { 
         //$data =  \Request::except(array('_token'));
         
        $inputs = $request->all();
         //dd($inputs);
        //exit;
        //echo $inputs['data'];
        $get_data=checkSignSalt($inputs['data']);

          
        $user_id=$get_data['user_id'];    
        $user_obj = User::findOrFail($user_id);

        $icon = $request->file('user_image');
        
                 
        if($icon){

            \File::delete(public_path('/upload/').$user_obj->user_image);            

            //$tmpFilePath = public_path().'/upload/';
            $tmpFilePath = public_path('/upload/');

            $hardPath =  Str::slug($get_data['name'], '-').'-'.md5(time());

            $img = Image::make($icon);

            $img->fit(250, 250)->save($tmpFilePath.$hardPath.'-b.jpg');
            //$img->fit(80, 80)->save($tmpFilePath.$hardPath. '-s.jpg');

            $user_obj->user_image = $hardPath.'-b.jpg';
        }
        
        
        $user_obj->name = $get_data['name'];          
        $user_obj->email = $get_data['email']; 
        $user_obj->phone = $get_data['phone'];
        
        if($get_data['password'])
        {
            $user_obj->password = bcrypt($get_data['password']);
        }         
       
        $user_obj->save();

        $user_id=$get_data['user_id'];    
        $user = User::findOrFail($user_id);

        if($user->user_image!='')
        {
            $user_image=\URL::asset('upload/'.$user->user_image);
        }
        else
        {
            $user_image=\URL::asset('upload/profile.jpg');
        }


        $response[] = array('user_image' => $user_image,'msg' => trans('words.successfully_updated'),'success'=>'1');
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
    }

    public function user_favourite_songs()
    {   
        
        $get_data=checkSignSalt($_POST['data']);

        $user_id=$get_data['user_id'];

        $data_list= Favourite::where('user_id',$user_id)->orderby('id','DESC')->paginate($this->pagination_limit);

        $total_records=Favourite::where('user_id',$user_id)->count();

        if($total_records > 0)
        {

            foreach($data_list as $obj_data)
            { 
                $song_title= Songs::getSongInfo($obj_data->post_id,'song_title');
                $song_image= \URL::to('/'.Songs::getSongInfo($obj_data->post_id,'song_image'));
                $song_info= Songs::getSongInfo($obj_data->post_id,'song_info');
                $song_lyrics= Songs::getSongInfo($obj_data->post_id,'song_lyrics');
                $song_type= Songs::getSongInfo($obj_data->post_id,'song_type');

                if($song_type=="server_url")
                {
                    $song_url= Songs::getSongInfo($obj_data->post_id,'song_url');
                }
                else
                {
                    $song_url= \URL::to('/'.Songs::getSongInfo($obj_data->post_id,'song_url'));
                }
                
                $song_views= post_views_count($obj_data->post_id);
                $song_download= post_download_count($obj_data->post_id);
                $total_rate= PostRatings::getPostTotalRatings($obj_data->post_id);
                $favourite=check_favourite($obj_data->post_id,isset($get_data['user_id'])?$get_data['user_id']:"");

                $artist_ids= Songs::getSongInfo($obj_data->post_id,'artist_ids');

                if(!empty($artist_ids))
                {
                    foreach(explode(',',$artist_ids) as $artist_id)
                    {
                         $artist_name= Artists::getArtistsInfo($artist_id,'artist_name');

                         $artist_list1[]=array('artist_id'=>$artist_id,'artist_name'=>$artist_name);
                    }
                }
                else
                {
                    $artist_list1=array();
                }

                $response[]=array("song_id"=>$obj_data->post_id,"song_title"=>$song_title,"song_image"=>$song_image,"song_info"=>$song_info,"song_lyrics"=>$song_lyrics,"song_type"=>$song_type,"song_url"=>$song_url,"views"=>$song_views,"downloads"=>$song_download,"total_rate"=>$total_rate,"favourite"=>$favourite,"artist_list"=>$artist_list1);

                unset($artist_list);
            }
        }
        else
        {
            $response=array();
        }


         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'total_records' => $total_records,
            'status_code' => 200
        ));

    }

    public function user_reports()
    { 
         
        $get_data=checkSignSalt($_POST['data']);

        $user_id = $get_data['user_id'];
        $post_id = $get_data['post_id'];
        $message = $get_data['message'];
 
         
            $re_obj = new Reports;

            $re_obj->post_id = $post_id;
            $re_obj->user_id = $user_id;
            $re_obj->message = $message;
            $re_obj->date = strtotime(date('m/d/Y H:i:s'));
            $re_obj->save();

            $success=true;
            
        $response[]=array("msg"=>trans('words.reports_success'));
         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
        
    }

    public function user_suggestions(Request $request)
    { 
          
        $inputs = $request->all();
         
        $get_data=checkSignSalt($inputs['data']);
          
         
        $sugg_obj = new Suggestion;

        $image = $request->file('image');        
                 
        if($image){
            
            $tmpFilePath = public_path('/upload/');
            $hardPath =  'suggestion'.rand(0,999).'-'.md5(time());

            $img = Image::make($image);
            $img->save($tmpFilePath.$hardPath.'-b.jpg');
 
            $sugg_obj->image = $hardPath.'-b.jpg';
        }
        
        
        $sugg_obj->user_id = $get_data['user_id'];          
        $sugg_obj->title = addslashes($get_data['title']); 
        $sugg_obj->message = addslashes($get_data['message']);
        $sugg_obj->date = strtotime(date('m/d/Y H:i:s')); 
        $sugg_obj->save();

          
        $response[] = array('msg' => trans('words.suggestion_success'),'success'=>'1');

        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
    }
    

    public function account_delete()
    { 

        $get_data=checkSignSalt($_POST['data']);

        $user_id=$get_data['user_id']; 

        $fav_obj = Favourite::where('user_id',$user_id)->delete();
        $rate_obj = PostRatings::where('user_id',$user_id)->delete();
        $sugg_obj = Suggestion::where('user_id',$user_id)->delete();
        $rep_obj = Reports::where('user_id',$user_id)->delete();
        
        $user = User::findOrFail($user_id);   
       

         //Account Delete Email
         if(getenv("MAIL_USERNAME"))
         {
             $user_name=$user->name;
             $user_email=$user->email;
 
             $data_email = array(
                 'name' => $user_name,
                 'email' => $user_email
                 );    
 
             \Mail::send('emails.account_delete', $data_email, function($message) use ($user_name,$user_email){
                 $message->to($user_email, $user_name)
                 ->from(getcong('app_email'), getcong('app_name'))
                 ->subject(trans('words.user_dlt_email_subject'));
             });    
         }

         $user->delete();

        $response_arr[] = array('msg' => trans('words.user_dlt_success'),'success'=>'1');
         
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response_arr,
            'status_code' => 200
        ));        
        
    }

    public function check_user_plan()
    {
        $get_data=checkSignSalt($_POST['data']);

        $user_id=$get_data['user_id'];

        $user_info = User::findOrFail($user_id);
        $user_plan_id=$user_info->plan_id;
        $user_plan_exp_date=$user_info->exp_date;
 

        if($user_plan_id==0)
        {          
            //\Session::flash('flash_message', 'Login status reset!');
            $response = array('msg' => trans('words.select_sub_plan'),'success'=>'0');

            return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200,
            'success' => 1
            ));
        }
        else if(strtotime(date('m/d/Y'))>$user_plan_exp_date)
        {

                $plan_info = SubscriptionPlan::where('id',$user_plan_id)->first();

                $plan_id= $plan_info->id;
                $plan_name= $plan_info->plan_name;  
                $plan_duration= SubscriptionPlan::getPlanDuration($plan_info->id);
                $plan_price= $plan_info->plan_price;
                $plan_device_limit= $plan_info->plan_device_limit;   
                $ads_on_off= $plan_info->ads_on_off;
                $download_on_off= $plan_info->download_on_off;                 
                 
                
                $expired_on=date('d, M Y',$user_plan_exp_date);

                $response = array('current_plan'=>$plan_name,'expired_on'=>$expired_on,"plan_id"=>$plan_id,"plan_name"=>$plan_name,"plan_duration"=>$plan_duration,"plan_price"=>$plan_price,"plan_device_limit"=>$plan_device_limit,"ads_on_off"=>$ads_on_off,"download_on_off"=>$download_on_off,'msg' => trans('words.renew_sub_plan'),'success'=>'0');

                return \Response::json(array(            
                'ONLINE_MP3_APP' => $response,
                'status_code' => 200,
                'success' => 1
                ));
        }
        else
        {
                $plan_info = SubscriptionPlan::where('id',$user_plan_id)->first();

                $plan_id= $plan_info->id;
                $plan_name= $plan_info->plan_name;  
                $plan_duration= SubscriptionPlan::getPlanDuration($plan_info->id);
                $plan_price= $plan_info->plan_price;
                $plan_device_limit= $plan_info->plan_device_limit;   
                $ads_on_off= $plan_info->ads_on_off;
                $download_on_off= $plan_info->download_on_off;
                
                $expired_on=date('d, M Y',$user_plan_exp_date);

                $response = array('current_plan'=>$plan_name,'expired_on'=>$expired_on,"plan_id"=>$plan_id,"plan_name"=>$plan_name,"plan_duration"=>$plan_duration,"plan_price"=>$plan_price,"plan_device_limit"=>$plan_device_limit,"ads_on_off"=>$ads_on_off,"download_on_off"=>$download_on_off,'msg' => trans('words.my_subscription'),'success'=>'1');

                return \Response::json(array(            
                'ONLINE_MP3_APP' => $response,
                'status_code' => 200,
                'success' => 1
                ));
        }        
        
        
    }

    public function subscription_plan()
    {
        $get_data=checkSignSalt($_POST['data']);

        $plan_list = SubscriptionPlan::where('status','1')->orderby('id')->get(); 


        $settings = Settings::findOrFail('1'); 

        $currency_code=$settings->currency_code;
        
        if(count($plan_list) > 0)
        {
            foreach($plan_list as $plan_data)
            {
                    $plan_id= $plan_data->id;
                    $plan_name= $plan_data->plan_name;  
                    $plan_duration= SubscriptionPlan::getPlanDuration($plan_data->id);
                    $plan_price= $plan_data->plan_price;
                    $plan_device_limit= $plan_data->plan_device_limit;   
                    $ads_on_off= $plan_data->ads_on_off;
                    $download_on_off= $plan_data->download_on_off;                 
                    
                    $response[]=array("plan_id"=>$plan_id,"plan_name"=>$plan_name,"plan_duration"=>$plan_duration,"plan_price"=>$plan_price,"plan_device_limit"=>$plan_device_limit,"ads_on_off"=>$ads_on_off,"download_on_off"=>$download_on_off,"currency_code"=>$currency_code);   
            }    
        }
        else
        {
            $response=array();
        }

        

        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200,
            'success' => 1
        ));
    }

     
    public function transaction_add()
    {
        $get_data=checkSignSalt($_POST['data']);

        $plan_id=$get_data['plan_id'];
        $user_id=$get_data['user_id'];
        $payment_id=$get_data['payment_id'];
        $payment_gateway=$get_data['payment_gateway'];

        $plan_info = SubscriptionPlan::where('id',$plan_id)->where('status','1')->first();
        $plan_name=$plan_info->plan_name;
        $plan_days=$plan_info->plan_days;
        $plan_amount=$plan_info->plan_price;

        //User info update        
           
        $user = User::findOrFail($user_id);

        $user_email=$user->email;

        $user->plan_id = $plan_id;                    
        $user->start_date = strtotime(date('m/d/Y'));             
        $user->exp_date = strtotime(date('m/d/Y', strtotime("+$plan_days days")));
                   
        $user->plan_amount = $plan_amount;         
        $user->save();

        //Check duplicate
        $trans_info = Transactions::where('user_id',$user_id)->where('payment_id',$payment_id)->first();

        if($trans_info=="")
        {
            //Transactions info update
            $payment_trans = new Transactions;

            $payment_trans->user_id = $user_id;
            $payment_trans->email = $user_email;
            $payment_trans->plan_id = $plan_id;
            $payment_trans->gateway = $payment_gateway;
            $payment_trans->payment_amount = $plan_amount;
            $payment_trans->payment_id = $payment_id;
            $payment_trans->date = strtotime(date('m/d/Y H:i:s'));                    
            $payment_trans->save();
        }

        $user_info = User::findOrFail($user_id);
        $user_plan_exp_date=$user_info->exp_date;

        $expired_on=date('d, M Y',$user_plan_exp_date);

        $response[] = array('expired_on' => $expired_on,'msg' => trans('words.payment_success'),'success'=>'1');
        
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200,
            'success' => 1
        ));
    }

    public function transaction_list()
    {
        $get_data=checkSignSalt($_POST['data']);

        
        $user_id=$get_data['user_id'];
        
        $transactions_list = Transactions::where('user_id',$user_id)->orderby('id')->get(); 
 
        $settings = Settings::findOrFail('1'); 

        $currency_code=$settings->currency_code;
        
        if(count($transactions_list) > 0)
        {
            foreach($transactions_list as $transaction_data)
            {
                    $plan_name= SubscriptionPlan::getSubscriptionPlanInfo($transaction_data->plan_id,'plan_name');  
                    
                    $payment_amount= $transaction_data->payment_amount;
                    $gateway= $transaction_data->gateway;   
                    $payment_id= $transaction_data->payment_id;
                    $date= date('M d Y h:i A',$transaction_data->date);                 
                    
                    $response[]=array("plan_name"=>$plan_name,"payment_amount"=>$payment_amount,"gateway"=>$gateway,"payment_id"=>$payment_id,"date"=>$date,"currency_code"=>$currency_code);   
            }    
        }
        else
        {
            $response=array();
        }
        
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200,
            'success' => 1
        ));
    }

    public function stripe_token_get()
    {

        $get_data=checkSignSalt($_POST['data']);

        $amount=$get_data['amount'];

 
        \Stripe\Stripe::setApiKey(getPaymentGatewayInfo(2,'stripe_secret_key'));


        $customer = \Stripe\Customer::create();
        $ephemeralKey = \Stripe\EphemeralKey::create(
            ['customer' => $customer->id],
            ['stripe_version' => '2020-08-27']
          );

        $currency_code=getcong('currency_code')?getcong('currency_code'):'USD';

        //$amount=10;

        $intent = \Stripe\PaymentIntent::create([
                'amount' => $amount * 100,
                'currency' => $currency_code,
            ]);

        if (!isset($intent->client_secret))
        {
            $response[]=array("msg"=>"The Stripe Token was not generated correctly",'success'=>'0');
        }
        else
        {
            $id = $intent->id;

            $client_secret = $intent->client_secret;
            $ephemeralKey = $ephemeralKey->secret;
            $customer_id = $customer->id;

            $response[]=array("id"=>$id,"stripe_payment_token"=>$client_secret,'ephemeralKey' =>$ephemeralKey,"customer" => $customer_id,"msg"=>"Stripe Token",'success'=>'1');
        }   
        

          return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));  
    }

    public function get_braintree_token()
    {


        require_once(base_path() . '/public/paypal_braintree/lib/Braintree.php');

        $mode=getPaymentGatewayInfo(1,'mode');
        
        if($mode=="sandbox")
        {
            $environment='sandbox';
        }
        else
        {
            $environment='production';
        }


        $merchantId=getPaymentGatewayInfo(1,'braintree_merchant_id');
        $publicKey=getPaymentGatewayInfo(1,'braintree_public_key');
        $privateKey=getPaymentGatewayInfo(1,'braintree_private_key');
 

        $gateway = new \Braintree\Gateway([
                        'environment' => $environment,
                        'merchantId' => $merchantId,
                        'publicKey' => $publicKey,
                        'privateKey' => $privateKey
                        ]);


        $clientToken = $gateway->clientToken()->generate();

        $response[] = array('client_token' => $clientToken,'msg' => 'Token created','success'=>'1');

           return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
             ));

    }  

    public function braintree_checkout()
    {

        $get_data=checkSignSalt($_POST['data']); 
         
        require_once(base_path() . '/public/paypal_braintree/lib/Braintree.php');

        $mode=getPaymentGatewayInfo(1,'mode');
        
        if($mode=="sandbox")
        {
            $environment='sandbox';
        }
        else
        {
            $environment='production';
        }
 

        $merchantId=getPaymentGatewayInfo(1,'braintree_merchant_id');
        $publicKey=getPaymentGatewayInfo(1,'braintree_public_key');
        $privateKey=getPaymentGatewayInfo(1,'braintree_private_key');
        $merchantAccountId=getPaymentGatewayInfo(1,'braintree_merchant_account_id');

        $gateway = new \Braintree\Gateway([
                        'environment' => $environment,
                        'merchantId' => $merchantId,
                        'publicKey' => $publicKey,
                        'privateKey' => $privateKey
                        ]);

        $payment_amount=$get_data['payment_amount'];
        $payment_nonce=$get_data['payment_nonce'];

        $result = $gateway->transaction()->sale([
          'amount' => $payment_amount,
          'paymentMethodNonce' => $payment_nonce,
          'merchantAccountId' => $merchantAccountId,
          'options' => [
            'submitForSettlement' => True
          ]
        ]);

        //echo $result->transaction->id;
        
        //dd($result);exit;

        if ($result->success) {

 
            $transaction_id= $result->transaction->id;

            $response[] = array('transaction_id' => $transaction_id,'msg' => 'Transaction successful','success'=>'1');

        }
        else
        {
            $response[] = array('msg' => 'Transaction failed','success'=>'0');
        }

           return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
             ));

    }

    public function razorpay_order_id_get()
    {

        $get_data=checkSignSalt($_POST['data']);

        $user_id=$get_data["user_id"];
        $amount=$get_data['amount']; 

        $razor_key=getPaymentGatewayInfo(3,'razorpay_key');
        $razor_secret=getPaymentGatewayInfo(3,'razorpay_secret');

        $api = new Api($razor_key, $razor_secret);

        $order = $api->order->create(array('receipt' => 'rcptid_'.$user_id, 'amount' => $amount, 'currency' => 'INR'));

        $orderId = $order['id'];
         
        $response[]=array("order_id"=>$orderId,"msg"=>"Order ID created",'success'=>'1');
         
          return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));  
    }
    
    public function payumoney_hash_generator()
    {
        
        $get_data=checkSignSalt($_POST['data']); 

        $hashdata=$get_data["hashdata"];
        $salt=getPaymentGatewayInfo(5,'payu_salt');
         
        /***************** DO NOT EDIT ***********************/
        $payhash_str = $hashdata.$salt;

        
        $hash = strtolower(hash('sha512', $payhash_str));
        /***************** DO NOT EDIT ***********************/

        $response[]=array("payu_hash"=>$hash,"msg"=>"Hash created",'success'=>'1');
         
          return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
        ));
         
    }
  

    public function user_device_limit_reached()
    {
        $get_data=checkSignSalt($_POST['data']); 
 
       $user_id=$get_data['user_id'];
       $plan_id=$get_data['plan_id'];
         
        if(user_device_limit_reached($user_id,$plan_id))
        {                 
            $response[] = array('msg' => trans('words.user_device_limit_reached'),'success'=>'1');
        }
        else
        {
            $response[] = array('msg' => trans('words.no_limit_reached'),'success'=>'0');
        }

        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
             ));
    }

    public function user_active_device_list()
    {
       $get_data=checkSignSalt($_POST['data']); 

       $user_id=$get_data['user_id'];
        
       $device_list= UsersDeviceHistory::where('user_id','=',$user_id)->orderBy('id','DESC')->get();

       if ($device_list->count()) 
       {
            foreach($device_list as $device_data)
            { 
                $user_device_date_time= date('m/d/Y h:i:s A', $device_data->date_time);

              $response[] = array('user_id' => $device_data->user_id,'device_name' => $device_data->user_device_name,'user_session_name' => $device_data->user_session_name,'user_device_date_time' => $user_device_date_time,'success'=>1);
            }
       }
       else
       {
           $response[] = array('msg' => trans('words.no_data_found'),'success'=>0);
       }

        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200
             ));
    }

    public function logout_user_remotely()
    {
        $get_data=checkSignSalt($_POST['data']); 

        $user_id=$get_data['user_id'];
        $user_session_name=$get_data['user_session_name'];
        
        //Push Notification on Mobile
        $content = array("en" =>  trans('words.logout_device_remotely'));

        $fields = array(
                'app_id' => getcong('onesignal_app_id'),
                'included_segments' => array('all'),                                            
                'data' => array("foo" => "bar", "logout_remote"=>"1"),
                'filters' => array(array('field' => 'tag', 'key' => 'user_session', 'relation' => '=', 'value' => $user_session_name)),
                'headings'=> array("en" => getcong('app_name')),
                'contents' => $content,
                'content_available' =>true,
                'apns-priority' =>5  
            );

        $fields = json_encode($fields);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Authorization: Basic '.getcong('onesignal_rest_key')));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $notify_res = curl_exec($ch);  

        curl_close($ch);
          
        //Delete session file
        \Session::getHandler()->destroy($user_session_name);
         
        $user_device_obj = UsersDeviceHistory::where('user_id',$user_id)->where('user_session_name',$user_session_name);
        $user_device_obj->delete();  

        //Device List
        $device_list= UsersDeviceHistory::where('user_id','=',$user_id)->orderBy('id','DESC')->get();

       if ($device_list->count()) 
       {
            foreach($device_list as $device_data)
            { 
                $user_device_date_time= date('m/d/Y h:i:s A', $device_data->date_time);

              $response[] = array('user_id' => $device_data->user_id,'device_name' => $device_data->user_device_name,'user_session_name' => $device_data->user_session_name,'user_device_date_time' => $user_device_date_time,'msg' => 'User successfully logout remotely');
            }
       }
       else
       {
           $response[] = array('msg' => trans('words.no_data_found'));
       } 

       //Device limit check
        $user = User::findOrFail($user_id);       
        $plan_id=$user->plan_id;
        if(user_device_limit_reached($user_id,$plan_id))
        {                 
            $device_limit_reached = "true";
        }
        else
        {
            $device_limit_reached = "false";
        }   
         
        return \Response::json(array(            
            'ONLINE_MP3_APP' => $response,
            'status_code' => 200,
            'success' =>$device_limit_reached
             ));
    }

    public function create_instamojo_orderid()
    { 
        $payment_mode=getPaymentGatewayInfo(9,'mode');
        $client_id = getPaymentGatewayInfo(9,'instamojo_client_id');
        $client_secret = getPaymentGatewayInfo(9,'instamojo_client_secret');  
        
        $get_data=checkSignSalt($_POST['data']);

        $user_id=$get_data["user_id"];
        $plan_id=$get_data['plan_id']; 
 

         $currency_code=getcong('currency_code')?getcong('currency_code'):'USD';
         
         if($payment_mode=="live")
         {
            $payment_oauth_url="https://api.instamojo.com/oauth2/token/";
            $payment_url="https://api.instamojo.com/v2/payment_requests/";
            $order_create_url="https://api.instamojo.com/v2/gateway/orders/payment-request/";

            $redirect_url="https://api.instamojo.com/integrations/android/redirect/";
         }
         else
         {
            $payment_oauth_url="https://test.instamojo.com/oauth2/token/";
            $payment_url="https://test.instamojo.com/v2/payment_requests/";
            $order_create_url="https://test.instamojo.com/v2/gateway/orders/payment-request/";

            $redirect_url="https://test.instamojo.com/integrations/android/redirect/";
         }

           
          
         $plan_info = SubscriptionPlan::where('id',$plan_id)->first();                 
         $plan_name=$plan_info->plan_name;         
         $plan_amount=$plan_info->plan_price;
  
        $user = User::findOrFail($user_id);

        $name = $user->name;
        $email= $user->email;
        $phone= $user->phone?$user->phone:'';

        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $payment_oauth_url);     
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

        $payload = Array(
            'grant_type' => 'client_credentials',
            'client_id' => $client_id,
            'client_secret' => $client_secret
          );

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
        $response = curl_exec($ch);
        curl_close($ch); 

        //dd($response);
        //exit;
        $token_obj=json_decode($response);
        
        $access_token=$token_obj->access_token;
        
        $success_url=\URL::to('instamojo/success/');

        $ch1 = curl_init();

        curl_setopt($ch1, CURLOPT_URL, $payment_url);
        curl_setopt($ch1, CURLOPT_HEADER, FALSE);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch1, CURLOPT_HTTPHEADER,array('Authorization: Bearer '.$access_token));

        $payload1 = array(
          'purpose' => $plan_name,
          'amount' => $plan_amount,
          'buyer_name' => $name,
          'email' => $email,
          'phone' => $phone,
          'redirect_url' => $redirect_url,
          'send_email' => 'False',
          'send_sms' => 'False',
          'allow_repeated_payments' => 'False',
        );

        curl_setopt($ch1, CURLOPT_POST, true);
        curl_setopt($ch1, CURLOPT_POSTFIELDS, http_build_query($payload1));
        $response1 = curl_exec($ch1);
        curl_close($ch1);

        //dd($response1);
        //exit;

        $payment_obj=json_decode($response1);

        $paymentId=$payment_obj->id; 
        //$status=$payment_obj->status; 
        //$longurl=$payment_obj->longurl; 
 
        $ch2 = curl_init();

        curl_setopt($ch2, CURLOPT_URL, $order_create_url);
        curl_setopt($ch2, CURLOPT_HEADER, FALSE);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch2, CURLOPT_HTTPHEADER,array('Authorization: Bearer '.$access_token));

        $payload2 = Array(
            'id' => $paymentId
        );

        curl_setopt($ch2, CURLOPT_POST, true);
        curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($payload2));
        $response2 = curl_exec($ch2);
        curl_close($ch2);  

        $order_obj=json_decode($response2);

        $order_id=$order_obj->order_id; 
        $name=$order_obj->name;
        $email=$order_obj->email;
        $amount=$order_obj->amount; 
         
         $response_arr[] = array('instamojo_order_id'=>$order_id,'name'=>$name,'email'=>$email,'amount'=>$amount,'msg' => 'Order ID created','success'=>'1');
            
         return \Response::json(array(            
            'ONLINE_MP3_APP' => $response_arr,
             'status_code' => 200
        ));    
    }
}
