<?php
class DB_USERINFO
{
    public $DB = false;
    public $tablename = 'userinfo';

    use BASICDB;
    public function __construct($DB = false)
    {
        if (!$DB) {
            $DB = DB();
        }
        if (!$DB) {
            return false;
        }
        if ($DB) {
            $this->DB = $DB;
        }
        if (!$this->tablename) {
            return false;
        }

    }
    function NewUserInfo($targetuserlogin, $fullname = '', $firstname = '', $middlename = '', $lastname = '', $landline = '', $mobile = '', $email = '', $altemail = '', $altlandline = '', $altmobile = '', $credit_card = '', $bankdetails = '[]', $facebookurl = '', $photourl = '[]', $otherdetails = '[]', $addresses = '[]')
    {
        if (!$targetuserlogin) {
            return false;
        }
        $usercheck = DBQUERY()->USERS()->getDetailsbyUIDorHashkey($targetuserlogin)['uid'] ?? false;
        if (!$usercheck) {
            return false;
        }
        $hashkey = $this->GenerateNewHash();
        if ($bankdetails) {
            if (is_array($bankdetails)) {
                $bankdetails = json_encode($bankdetails);
            }
        } else {
            $bankdetails = '[]';
        }
        if ($otherdetails) {
            if (is_array($otherdetails)) {
                $otherdetails = json_encode($otherdetails);
            }
        } else {
            $otherdetails = '[]';
        }
        if ($addresses) {
            if (is_array($otherdetails)) {
                $otherdetails = json_encode($otherdetails);
            }
        } else {
            $addresses = '[]';
        }


        $data = [
            'hashkey' => $hashkey,
            'targetuserlogin' => $targetuserlogin,
            'fullname' => $fullname,
            'firstname' => $firstname,
            'middlename' => $middlename,
            'lastname' => $lastname,
            'landline' => $landline,
            'mobile' => $mobile,
            'email' => $email,
            'altemail' => $altemail,
            'altlandline' => $altlandline,
            'altmobile' => $altmobile,
            'credit_card' => $credit_card,
            'bankdetails' => $bankdetails,
            'facebookurl' => $facebookurl,
            'photourl' => $photourl,
            'otherdetails' => $otherdetails,
            'addresses' => $addresses

        ];
        $insert = $this->InsertIntoDB($data);
        return $insert;
    }
    function GetbyTargetUserUID($useruidorhashkey, $fieldstoselect = '')
    {
        $result = $this->ListbyTargetUserUID($useruidorhashkey, $fieldstoselect);

        return $result[0] ?? [];
    }
    function ListbyTargetUserUID($useruidorhashkey, $fieldstoselect = '')
    {
        if (!$useruidorhashkey) {
            return false;
        }
        if (!is_numeric($useruidorhashkey)) {
            $useruidorhashkey = DB_USERS($this->DB)->GetUserUIDbyHashkey($useruidorhashkey) ?? false;
        }

        if (!$useruidorhashkey || !is_numeric($useruidorhashkey)) {
            return false;
        }
        return $this->ListFromDB(['targetuserlogin' => $useruidorhashkey], [], $fieldstoselect);
    }

    function ModifybyTargetUserUID($targetuserUID, $data)
    {
        if (!$targetuserUID || !$data) {
            return false;
        }
        $wherearray = ['targetuserlogin' => $targetuserUID];
        return $this->ModifySingleRowwithVerification($data, $wherearray);
    }
    function GetAddresses($targetuserUID)
    {
        if (!$targetuserUID) {
            return false;
        }
        
        $details = $this->GetbyTargetUserUID($targetuserUID);
        if (!$details) {
            return false;
        }
        $addresses = $details['addresses'] ?? null;
        if ($addresses) {
            $addresses = tryjsondecode($addresses, true);
        }
        return $addresses;
    }
    function GetLastAddress($targetuserUID)
    {
        $details = $this->GetAddresses($targetuserUID);
        if ($details === false) {
            return false;
        }
        if ($details === null) {
            return null;
        }
        $details[count($details) - 1]['key'] = count($details) - 1;
        return $details[count($details) - 1];
    }
    function getDefaultAddress($targetuserUID)
    {
        if (!$targetuserUID) {
            return false;
        }
        $details = $this->GetAddresses($targetuserUID);
        if (!$details) {
            return false;
        }
        if (count($details) === 1) {
            $details['key'] = 0;
            return $details[0];
        }
        foreach ($details as $key => $value) {
            if ($value['default']) {
                $value['key'] = $key;
                return $value;
            }
        }
        return null;
    }
    function SetAddresses($targetuserUID, $newaddresses)
    {

        if (!$targetuserUID || !is_numeric($targetuserUID)) {
            return false;
        }

        $data = [];
        if (is_array($newaddresses)) {
            $newaddresses = array_values($newaddresses);
            $newaddresses = json_encode($newaddresses);
        }

        $data['addresses'] = $newaddresses;
        return $this->ModifybyTargetUserUID($targetuserUID, $data);
    }
    function AddAddress($targetuserUID, $address, $addressname, $contactname, $contactnumber, $default = false)
    {
        if (!$targetuserUID || !$address || !$addressname || !$contactname || !$contactnumber) {
            return false;
        }
        if ($default === 'true') {
            $default = true;
        } elseif ($default === 'false') {
            $default = 0;
        }

        $data = $this->GetAddresses($targetuserUID);
        if ($data === false) {
            return false;
        }

        if (!$data) {
            $data = [];
        }
        if ($default) {
            foreach ($data as $key => $value) {
                $data[$key][4] = false;
            }
        }
        $data[] = [$address, $addressname, $contactname, $contactnumber, $default];

        $submit = $this->SetAddresses($targetuserUID, $data);
        if (!$submit) {
            return false;
        }
        return $this->GetLastAddress($targetuserUID);
    }
    function RemoveAddress($targetuserUID, $index)
    {
        if (!$targetuserUID || !is_numeric($index)) {
            return false;
        }
        $addressees = $this->GetAddresses($targetuserUID);
        if ($addressees === false) {
            return false;
        }
        if (!isset($addressees[$index])) {
            return false;
        }
        unset($addressees[$index]);
        $addressees = array_values($addressees);
        return $this->SetAddresses($targetuserUID, $addressees);
    }
    function ModifyAddressbyIndex($targetuserUID, $index,$name=null,$address=null,$contactname=null,$contactnumber=null,$default=null)
    {
        if(!$name && !$address  && !$contactname && !$contactnumber && $default===null) {return false;}
        if (!$targetuserUID || !is_numeric($index)) {
            return false;
        }
        $addressees = $this->GetAddresses($targetuserUID);
        if ($addressees === false) {
            return false;
        }
        if (!isset($addressees[$index])) {
            return false;
        }
        $dataname= $addressees[$index][1] ?? false;
        $dataaddress = $addressees[$index][0] ?? false;
        $datacontactname =$addressees[$index][2] ?? false;
        $datacontactnumber = $addressees[$index][3] ?? false;
        $datadefault  = $addressees[$index][4] ?? false;

        $finalname = $name ?? $dataname;
        $finaladdress = $address ?? $dataaddress;
        $finalcontactname = $contactname ?? $datacontactname;
        $finaldatacontactnumber =$contactnumber ?? $datacontactnumber;
        if($default !==null){
            $finaldefault = $default;
        }else{
            $finaldefault= $datadefault;
        }

        if ($finaldefault) {
            foreach ($addressees as $key => $value) {
                $addressees[$key][4] = false;
            }
        }

        $addressees[$index]=[$finaladdress,$finalname,$finalcontactname,$finaldatacontactnumber,$finaldefault];
        $addressees = array_values($addressees);
        return $this->SetAddresses($targetuserUID, $addressees);
    }
    function SetAddressAsDefault($targetuserUID,$index){
        return $this->ModifyAddressbyIndex($targetuserUID, $index,null,null,null,null,true);
    }
    function EmptyAddresses($targetuserUID)
    {
        return $this->SetAddresses($targetuserUID, '');
    }
    function ShowAddressFormat()
    {
        return ['Address', 'Address Name', 'Contact Name', 'Contact Number', 'default'];
    }
}

function DB_USERINFO($db = false)
{
    return new DB_USERINFO($db);
}

function USERINFO_MULTIPLESEARCH($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false)
{

    return new USERINFO_QUICK_MULTIPLESEARCH($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata);
}

class USERINFO_QUICK_MULTIPLESEARCH
{
    use DBClassSearch;
    public $data;

    public $tablename = 'users';

    private $parentidresults = [];
    public $DB;

    public function __construct($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false)
    {
        return $this->initialize($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata);
    }

    function FindName($uid)
    {
        return $this->Find($fieldname = 'uid', $contenttosearch = $uid, $exact = true)[0]['name'];
    }

}