<?php


namespace App\Http\Controllers;
use App\Repositories\UserRepository;
use App\User;
use Auth;
use Request;
use Validator;


class UserDataController extends Controller
{

    protected $userRepo;

    public function __construct(UserRepository $userRepo)
    {
        $this->middleware('auth');
        $this->userRepo = $userRepo;
    }


    public function userEdit(){
        $user = Auth::user();

        /*if(!$user->account){
            $user->account = new UserAccount();
        }*/
        $data = [
            'user' => $user,
        ];
        return view('user.edit', $data);
    }


    public function userEditStore(){

        $user = Auth::user();
        /*if(!$user->account){
            $user->account = new UserAccount();
        }*/
        $rules = array(
            'salutation' => 'required',
            'first_name'=>'required',
            'last_name'=>'required',
            'address'=>'required',
            'zipcode'=>'required',
            'city' => 'required',
            'email' => 'required|string|email|max:255|exists:users,email',
            'email-confirm' => 'required|same:email',
        );
        if(!Request::get('same_as_billing')){
            $rules = array_merge($rules, [
                'shipping_firstname'=>'required',
                'shipping_lastname'=>'required',
                'shipping_address'=>'required',
                'shipping_zipcode'=>'required',
                'shipping_city' => 'required',
                'shipping_salutation' => 'required'

            ]);
        }
        $data = [
            'user' => $user,
        ];
        $validator = Validator::make(Request::all(), $rules);

        if ($validator->fails()) {

            return view('user.edit', $data)->withErrors($validator);

        } else {
            $this->userRepo->update(Request::all());
            \Session()->flash('alert-save', true);
            return redirect('/user/edit');
        }
    }


    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function userDataStore(){

        $user = User::findOrFail(Auth::user()->id);
        if(!$user->account){
            $user->account = new UserAccount();
        }
        $rules = array(
            'salutation' => 'required',
            'last_name' => 'required|max:255',
            'country_id' => 'required|integer|min:1',
            'email' => 'required|string|email|max:255|exists:users,email',
            'email-confirm' => 'required|same:email',
        );
        if($user->active == 0){
            $rules['accepted_data_protection'] = 'required';
            $rules['accepted_active'] = 'required';
        }

        if(Request::get('company') == 1){
            $rules['company_name'] = 'required|max:255';
            $rules['company_country_id'] = 'required|integer|min:1';
        }

        $data = [
            'user' => $user,
        ];

        $validator = Validator::make(Request::all(), $rules);

        if ($validator->fails()) {

            // get the error messages from the validator
            $messages = $validator->messages();
            // redirect our user back to the form with the errors from the validator
            return view('user.edit', $data)->withErrors($validator);

        } else {
            $this->userRepo->update(Request::all());

            if($user->active == 0) {
                $account = $user->account;
                $account->data_protection = now();
                $account->save();

                $user->active = 1;
                $user->active_date = now();
                $user->save();
            }

            if(Request::get('accepted_active') == "on"){
                $user->agreement = now();
            }else{
                $user->agreement = null;
            }


            \Session()->flash('alert-save', true);
            return redirect('/home');
        }
    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function userDataAcceptedForm(){
        $user = Auth::user();

        if(Request::get('accepted_active') == "on"){
            $user->agreement = now();
        }else {
            $user->agreement = null;
        }

        $user->save();
        \Session()->flash('alert-save', true);
        return redirect('/home');
    }


    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function userDataFree(){
        $user = Auth::user();
        $user->active = 1;
        $user->active_date = now();
        $user->save();
        return redirect('/home');

    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function userDataFreeForm(){
        $user = Auth::user();

        $rules = array(
            'accepted_data_protection' => 'required'
        );

        $data = [
            'user' => $user,
        ];
        $validator = Validator::make(Request::all(), $rules);

        if ($validator->fails()) {
            // get the error messages from the validator
            $messages = $validator->messages();
            // redirect our user back to the form with the errors from the validator
            return view('home', $data)->withErrors($validator);
        } else {
            $account = $user->account;
            $account->data_protection = now();
            $account->save();

            if(Request::get('accepted_active') == "on"){
                $user->agreement = now();
            }else{
                $user->agreement = null;
            }



            $user->active = 1;
            $user->active_date = now();
            $user->save();

        }
        return redirect('/home');

    }

}