<?php
namespace App\Services;


use App\Mail\MailCheckout;
use App\Mail\MailInfo;
use App\Models\ShoppingUser;
use App\Services\Shop;
use App\User;
use Auth;
use Illuminate\Support\Facades\Mail;



class CustomerPriority
{

    protected static $start_number = 1000;
    protected static $user_notice_key = 'like';

    public static function checkForAll(){
        //only extern no members with no numbers
        $shopping_users = ShoppingUser::where('auth_user_id', '=', NULL)->where('number', '=', NULL)->orderBy('created_at', 'ASC')->get();
        foreach ($shopping_users as $shopping_user){
            if($shopping_user->shopping_order && $shopping_user->shopping_order->user_shop){
                self::checkOne($shopping_user);
            }
        }
        return $shopping_users;
    }

    public static function checkOne($shopping_user, $mail=false, $newCustomer = true){
        //look for entry
        if(self::entryExists($shopping_user)){
            return 'exists';
        }
        if(self::entryLike($shopping_user)){
            if($mail){ //send mail
                Mail::to(config('app.info_mail'))->send(new MailInfo($shopping_user, 'check_is_like_customer'));
            }
            return 'like';
        }
        if($newCustomer){
            self::newCustomer($shopping_user);
        }
        return 'new';

    }

    public static function setIsLike($shopping_user, $set_like_shopping_user, $send_member_mail, $change_shopping_user=false)
    {
        if ($shopping_user->id === $set_like_shopping_user->id) {
            //set new customer for shopping_user
            if($change_shopping_user){
                self::newCustomerNumber($shopping_user);
                $send_member_mail = false;
            }else{
                self::newCustomer($shopping_user);
            }
        } else {
            //set existing customer for shopping_user
            self::existingCustomer($shopping_user, $set_like_shopping_user);
        }
        self::clearIsLike($shopping_user);
        //SEND MAIL TO MEMBER
        if ($send_member_mail){
            if ($shopping_user->shopping_order && $shopping_user->shopping_order->shopping_payments) {
                Mail::to($shopping_user->member->email)->send(new MailCheckout($shopping_user->shopping_order->txaction, $shopping_user->shopping_order, $shopping_user->shopping_order->shopping_payments->last(), false, $shopping_user->shopping_order->mode));
            }
        }
        return true;
    }

    public static function newMemberForOrder($shopping_order, $member_id, Bool $all = false){
        if($all){
            if($shopping_order->shopping_user && $shopping_order->shopping_user->number){
                $shopping_users = ShoppingUser::where('number', '=', $shopping_order->shopping_user->number)->get();
                $new_number = self::nextNumber();
                foreach ($shopping_users as $shopping_user) {
                    self::changeCustomer($shopping_user, $member_id, $new_number);
                }
            }
        }else{
            $shopping_order->member_id = $member_id;
            $shopping_order->save();
            if($shopping_order->shopping_user){
                $shopping_order->shopping_user->member_id = $member_id;
                $shopping_order->shopping_user->number = self::nextNumber();
                $shopping_order->shopping_user->save();
            }
        }
    }

    public static function newMemberForCustomer($shopping_user, $member_id, Bool $all = false){
        if($all){
            if($shopping_user && $shopping_user->number){
                $shopping_users = ShoppingUser::where('number', '=', $shopping_user->number)->get();
                $new_number = self::nextNumber();
                foreach ($shopping_users as $s_user) {
                    self::changeCustomer($s_user, $member_id, $new_number);
                }
            }
        }else{
            $shopping_user->member_id = $member_id;
            $shopping_user->number = self::nextNumber();
            $shopping_user->save();
            if($shopping_user->shopping_order){
                $shopping_user->shopping_order->member_id = $member_id;
                $shopping_user->shopping_order->save();
            }
        }
    }

    public static function checkChangeOne($shopping_user, $data, $mail=false){

        $matches = [];
        $change = [];
        $ret = 'update';
        //email geändert
        if(isset($data['billing_email']) && $shopping_user->billing_email != $data['billing_email']){
            $found = ShoppingUser::where('auth_user_id', '=', NULL)
                ->where('number', '!=', NULL) //has number
                ->where('id', '!=', $shopping_user->id)
                ->where('billing_email', '=', $data['billing_email'])
                ->get()->pluck('number', 'id')->unique()->toArray();
            if($found && count($found)){
                foreach ($found as $key=>$val){
                    $matches[$key] = $val;
                }
                $ret = 'exists';
                $change['billing_email'] = $data['billing_email'];
            }
        }
        //Anschrift geändert
        if(isset($data['billing_lastname']) && isset($data['billing_zipcode']) && ($shopping_user->billing_lastname != $data['billing_lastname'] || $shopping_user->billing_zipcode != $data['billing_zipcode'])){
            $found = ShoppingUser::select('*')
                ->where('auth_user_id', '=', NULL)
                ->where('number', '!=', NULL) //has number
                ->where('id', '!=', $shopping_user->id)
                ->where('billing_lastname', '=', $data['billing_lastname'])
                ->where('billing_zipcode', '=', $data['billing_zipcode'])
                ->get()->pluck('number', 'id')->unique()->toArray();
            if($found && count($found)){
                foreach ($found as $key=>$val){
                    $matches[$key] = $val;
                }
                $ret = 'like';
                $change['billing_lastname'] = $data['billing_lastname'];
                $change['billing_zipcode'] = $data['billing_zipcode'];
            }
        }
        if($matches){
            $shopping_user->is_like = true;
            $shopping_user->setNotice(self::$user_notice_key, $matches);
            $shopping_user->save();
        }
        //look for entry
        if($matches && $mail){ //send mail
            Mail::to(config('app.info_mail'))->send(new MailInfo($shopping_user, 'change_is_like_customer', $change));
        }

        return $ret;

    }

    public static function checkNewOne($shopping_user, $mail=false){

        if(self::entryLike($shopping_user)){
            if($mail){ //send mail
                Mail::to(config('app.info_mail'))->send(new MailInfo($shopping_user, 'check_is_like_customer'));
            }
            //return 'like';
        }
        $shopping_user->number = self::nextNumber();
        $shopping_user->save();
        return true;
    }

    private static function entryExists($shopping_user){
        //check same email
        $matches = ShoppingUser::where('auth_user_id', '=', NULL)
            ->where('number', '!=', NULL) //has number
            ->where('id', '!=', $shopping_user->id)
            ->where('billing_email', '=', $shopping_user->billing_email)->get();

        if($matches && count($matches)){
            $match = $matches->last();
            $shopping_user->member_id = $match->member_id;
            $shopping_user->number = $match->number;
            $shopping_user->save();
            //has order
            if($shopping_user->shopping_order){
                $shopping_user->shopping_order->member_id = $match->member_id;
                $shopping_user->shopping_order->save();
            }
            return true;
        }
        return false;
    }

    private static function entryLike($shopping_user){
        //check same last name und PLZ
        $matches = ShoppingUser::select('*')
            ->where('auth_user_id', '=', NULL)
            ->where('number', '!=', NULL) //has number
            ->where('id', '!=', $shopping_user->id)
            ->where('billing_lastname', '=', $shopping_user->billing_lastname)
            ->where('billing_zipcode', '=', $shopping_user->billing_zipcode)
            ->get()->pluck('number', 'id')->unique()->toArray();
        if($matches && count($matches)){
            $shopping_user->is_like = true;
            $shopping_user->setNotice(self::$user_notice_key, $matches);
            $shopping_user->save();
            return true;
        }
        return false;
    }

    private static function newCustomer($shopping_user){
        if($shopping_user->shopping_order && $shopping_user->shopping_order->user_shop) {
            $member_id = $shopping_user->shopping_order->user_shop->user_id;
            $shopping_user->member_id = $member_id;
            $shopping_user->number = self::nextNumber();
            $shopping_user->save();
            $shopping_user->shopping_order->member_id = $member_id;
            $shopping_user->shopping_order->save();
        }
    }

    private static function newCustomerNumber($shopping_user)
    {
        \App\Services\Shop::newUserOrder($shopping_user->number);
        $shopping_user->number = self::nextNumber();
        $shopping_user->save();
        \App\Services\Shop::newUserOrder($shopping_user->number);

    }

    private static function changeCustomer($shopping_user, $member_id, $number){
        $old_number = $shopping_user->number;
        $shopping_user->member_id = $member_id;
        $shopping_user->number = $number;
        $shopping_user->save();
        if($shopping_user->shopping_order) {
            $shopping_user->shopping_order->member_id = $member_id;
            $shopping_user->shopping_order->save();
        }
        \App\Services\Shop::newUserOrder($old_number);
        \App\Services\Shop::newUserOrder($number);
    }

    private static function existingCustomer($shopping_user, $set_like_shopping_user){
        $old_number = $shopping_user->number;
        $shopping_user->member_id = $set_like_shopping_user->member_id;
        $shopping_user->number = $set_like_shopping_user->number;
        $shopping_user->save();
        if($shopping_user->shopping_order) {
            $shopping_user->shopping_order->member_id =  $set_like_shopping_user->member_id;
            $shopping_user->shopping_order->save();
        }
        \App\Services\Shop::newUserOrder($old_number);
        \App\Services\Shop::newUserOrder($set_like_shopping_user->number);
    }

    private static function clearIsLike($shopping_user){
        $shopping_user->is_like = false;
        $shopping_user->removeNotice(self::$user_notice_key);
        $shopping_user->save();

    }

    private static function nextNumber (){
        if(!$number = ShoppingUser::max('number')){
            $number = self::$start_number;
        }
        return $number+1;
    }

}
