<?php

namespace App\Http\Controllers;

use App\Models;
use Illuminate\Http\Request;
use Session;
use DB;


class ClickAppsController extends Controller
{
	public function index(Request $request)
	{
		$click_apps = DB::table('apps');
		$columns = [
			'app_name' => 'App Name',
			'operator' => 'Operator',
			'country' => 'Counry',
			'status' => 'Status'
		];
		if (!empty($request->filter) && !empty($request->keyword)) {
			$click_apps = $this->resolveAppQuery($click_apps, $request);
		}

		$click_apps = $click_apps->orderBy('apps.created_at', 'DESC')->paginate(20);

		$data = [
			'page_title' => 'Click Apps',
			'click_apps' => $click_apps,
			'columns' => $columns
		];

		return view('click_apps.index', $data);
	}

	public function create()
	{
		$data = $this->crudData();
		return view('click_apps.create', $data);
	}

	public function store(Request $request)
	{
		$request->validate($this->valdtePrams());
		DB::transaction(function () use ($request) {
			Models\ClickApps::create($request->only($this->whiteListCrudParams()));
		});
		Session::flash('success_message', 'App record created successfully');
		return redirect(url('clickapps/create'));
	}

	public function edit($id)
	{
		$clickapp =  Models\ClickApps::find($id);
		if (!$clickapp) {
			Session::flash('error_message', "Record with ID: {$id} not found");
			return redirect(url('clickapps'));
		}
		$data = $this->crudData();
		return view('click_apps.edit', array_merge($data, [
			'clickapp' => $clickapp
		]));
	}
	public function getShortCodes(){
        $data = [
            'page_title' => 'Click Short Codes'
        ];
        dd('This feature is not yet ready');
        return view('click_apps.short-codes', $data);
    }

	public function update(Request $request, $id)
	{
		$clickapp =  Models\ClickApps::find($id);
		if (!$clickapp) {
			Session::flash('error_message', "Record with ID: {$id} not found");
			return redirect(url('clickapps'));
		}
		$request->validate($this->valdtePrams());
		DB::transaction(function () use ($request, $clickapp) {
			$clickapp->update($request->only($this->whiteListCrudParams()));
		});
		Session::flash('success_message', 'App record updated successfully');
		return redirect(url('clickapps'));
	}

	public function destroy($id)
	{
		Models\ClickApps::destroy($id);
		if (request()->ajax()) {
			$res = ['code' => 1];
			return response()->json($res);
		}
		Session::flash('success_message', 'App successfully deleted!');
		return redirect(url('clickapps'));
	}

	private function crudData()
	{
		$click_apps = Models\ClickApps::get();
		$countries = Models\Country::pluck('en_short_name', 'alpha_2_code');
		$net_ops = Models\NetworkOps::pluck('name', 'name');
		$toll_types = ['YES' => 'YES', 'NO' => 'NO'];
		$status_types = ['ACTIVE' => 'ACTIVE', 'PENDING' => 'PENDING', 'CANCELLED' => 'CANCELLED', 'FINISHED' => 'FINISHED'];
		return [
			'page_title' => 'Click Apps',
			'click_apps' => $click_apps,
			'countries' => $countries,
			'net_ops' => $net_ops,
			'toll_types' => $toll_types,
			'status_types' => $status_types
		];
	}

	private function valdtePrams()
	{
		return [
			'app_name' => 'required',
			'app_type' => 'required',
			'client' => 'required',
			'code' => 'required',
			'country' => 'required',
			'operator' => 'required',
			'tollfree' => 'required',
			'app_path' => 'required',
			'launch_date' => 'required',
			'status' => 'required',
			'other_info' => 'required',
		];
	}
	private function whiteListCrudParams()
	{
		return [
			'app_name',
			'app_type',
			'client',
			'code',
			'country',
			'operator',
			'tollfree',
			'app_path',
			'launch_date',
			'status',
			'other_info',
		];
	}

	function resolveAppQuery($clients, $request)
	{
		switch ($request->filter) {
			case 'app_name':
				return $clients
					->where('app_name', 'like', "%$request->keyword%");
				break;
			case 'operator':
				return $clients
					->where('operator', 'like', "%$request->keyword%");
				break;
			case 'country':
				return $clients
					->where('country', 'like', "%$request->keyword%");
				break;
			case 'status':
				return $clients
					->where('status', 'like', "%$request->keyword%");
				break;
			default:
				# code...
				break;
		}
	}
}
