<?php
namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Validator;


class Media extends Controller
{
   public function addMedia(Request $request)
    {
        $memId = \App\Helper::getMemId();
        calculate_coc($memId);
        //$data = json_decode($request->getContent(), true);
        $data["channels"] = $request->input("channels");
        $data["memId"] = $request->input("memId");
        $validator = Validator::make($data, [
            'channels' => 'required',
            'memId' => 'int',
        ]);
        if ($validator->fails()) {
            $messages = $validator->messages();
            return response()->json([
               "code" => \App\RestApiResponseCodes::mandatoryParamsMissingCode,
                'message' => \App\RestApiResponseCodes::mandatoryParamsMissingMsg
                    ///'message' => $messages
			   /*  'status' => [
                    "code" => \App\RestApiResponseCodes::mandatoryParamsMissingCode,
                    'message' => \App\RestApiResponseCodes::mandatoryParamsMissingMsg
                    ///'message' => $messages
                ] */
            ]);
        }
        $channels = implode(", ", $data['channels']);
		/*if( strpos($channels, "email") !== false ){
			
			file_get_contents("https://www.steelmint.com/registrationAJAX.php?setUnsetBounce=1&m=0&mid=$memId");
		} else {
			
			file_get_contents("https://www.steelmint.com/registrationAJAX.php?setUnsetBounce=1&m=1&mid=$memId");
		}*/
		
        //$hasChannel = \App\Media::select('*')->where('memid', $memId)->where('channels', $channels)->first();
        $hasChannel = \App\Media::select('*')->where('memid', $memId)->first();
        if (!$hasChannel) {
            \App\Media::insert([
                'memid' => $memId,
                'f_date' => $GLOBALS['currDate'],
                'f_time' => $GLOBALS['currTime'],
                'channels' => $channels,
            ]);
            /* return response()->json([
                'status' => [
                    "code" => \App\RestApiResponseCodes::RequestSentSuccessfullyCode,
                    'message' => \App\RestApiResponseCodes::RequestSentSuccessfullyMsg
                ]
            ]); */
            
            //code to add/remove email in sendy list based on selected option
            if( strpos($channels, "email") !== false ){			
				file_get_contents("https://www.steelmint.com/registrationAJAX.php?resetAfterSetChannels=1&m=0&mid=$memId");
			} else {				
				file_get_contents("https://www.steelmint.com/registrationAJAX.php?resetAfterSetChannels=1&m=1&mid=$memId");
			}
           
           
			return response()->json([
                "code" => \App\RestApiResponseCodes::RequestSentSuccessfullyCode,
                'message' => \App\RestApiResponseCodes::RequestSentSuccessfullyMsg
            ]);
        } else {
            \App\Media::where('id', $hasChannel->id)->update([
                'updated_on'=>$GLOBALS['currDate']." ".$GLOBALS['currTime'],
                'channels' => $channels,
            ]);

           /*  return response()->json([
                'status' => [
                    "code" => \App\RestApiResponseCodes::RequestSentSuccessfullyCode,
                    'message' => \App\RestApiResponseCodes::RequestSentSuccessfullyMsg
                ]
            ]); */
           //code to add/remove email in sendy list based on selected option
            if( strpos($channels, "email") !== false ){			
				file_get_contents("https://www.steelmint.com/registrationAJAX.php?resetAfterSetChannels=1&m=0&mid=$memId");
			} else {				
				file_get_contents("https://www.steelmint.com/registrationAJAX.php?resetAfterSetChannels=1&m=1&mid=$memId");
			}
			 return response()->json([
                "code" => \App\RestApiResponseCodes::RequestSentSuccessfullyCode,
                'message' => \App\RestApiResponseCodes::RequestSentSuccessfullyMsg
                
            ]);
        }

    }

    public function getMedia(Request $request)
    {
        $memId = \App\Helper::getMemId();
        //$data = json_decode($request->getContent(), true);
        $channels = \App\Media::select('channels')->where('memid', $memId)->first();
        //$channels = (!empty($channels) ? explode(",", $channels) : []);
        $channels = (!empty($channels) ? $channels : []);
        //print_r($channels);
	   return response()->json([
            'status' => [
                "code" => \App\RestApiResponseCodes::RequestSentSuccessfullyCode,
                'message' => \App\RestApiResponseCodes::RequestSentSuccessfullyMsg,
                'data' =>  $channels
            ]
        ]);
    }


}
