import {ResponseT, TUser} from '../context/UserContext';
import axios from 'axios'

export const Axios = axios.create({
    //    baseURL: 'http://localhost/dog/php-dog/',
    //    baseURL: 'https://hope-fly.de/dog/php-dog/',
        baseURL: process.env.REACT_APP_PHP_ROOT,
    });
    

export const updateDog = async ({email, name, phone}:
    {email: string, name: string, phone: string}) => 
{
    try{
        const {data} = await Axios.post('updateDog.php',{
            email,
            name,
            phone 
        });
        console.log('Api updateDog');
        console.log(data);
        return data;
    }
    catch(err){
        return {success:0, message:'Server Error!'};
    }
}

export const getDog = async ( params: string[] | undefined) : Promise<ResponseT> =>
{
  var ret: ResponseT = {};
  let qrid = '';

  if(params !== undefined && params.length >= 2)
  {
    qrid = params[1];
  }
  if(params !== undefined && params.length == 1)
  {
    qrid = params[0];
  }

  try 
  {
    ret = await Axios.post<ResponseT>('getDog.php', 
    {
      qr_id: qrid
    });
    console.log('Api getDog');
    console.log(ret.data);

    return ret.data as ResponseT;
  } 
  catch (error:any) 
  {
        console.log('Api getDog error message: ', error.message);
        ret = 
        {
            success: 0,
            status: 500,
            message: error.message,
            fields: null,
            data: null
        };
        return ret;
  } 
}

export const registerUser = async ({email,password}:
    {email: string, password: string}) => 
{
    try{
        const {data} = await Axios.post('register.php',
        {
            email,
            password 
        });
        console.log('Api registerUser');
        console.log(data);
        return data;
    }
    catch(err)
    {
        return {success:0, message:'Server Error!'};
    }
}

export const updateQR = async ({qr_width_cm, qr_height_cm, qr_fontsize, qr_visible_items, qr_item_sequence}:
    {qr_width_cm: number, qr_height_cm: number, qr_fontsize: number, qr_visible_items: number, qr_item_sequence: number}) => 
{
    try
    {
        const {data} = await Axios.post('updateQR.php',
        {
            qr_width_cm,
            qr_height_cm,
            qr_fontsize,
            qr_visible_items,
            qr_item_sequence 
        });
        console.log('Api updateQR');
        console.log(data);
        return data;
    }
    catch(err)
    {
        return {success:0, message:'Server Error!'};
    }
}

export const loginUser = async ({email,password}:{email: string, password: string}) => 
{
    try
    {
        var postReturn:
        {   
            data:
            {
                success: number;
                message: string;
                data: TUser | null;
            } 
        } 
        = { data : {success: 0, message: '', data: null } };

        postReturn = await Axios.post('login.php',
        {
            email,
            password 
        });

        const {data} = postReturn;
        return data;
    }
    catch(err)
    {
        return {success:0, message:'Server Error!'};
    }
}

export const getProfilData = async () =>
{
    const profilData =  await Axios.post('getProfilData.php');
    const {data} = profilData;
    if(data.success && data.data)
    {
        return data;
    }
    else
    {
        return null;
    }
}

export const getUser = async () => 
{
    const res = await Axios.get('getUser.php');
    console.log("Api getUser");
    console.log(res);

    const {data} = res;
    return data;
}

export const upload = (file: File, qr: string = '', onUploadProgress: any = null): Promise<any> => 
{
    let formData = new FormData();
  
    formData.append("file", file);
    formData.append("submit", "1");
    if(qr !== '')
    {
        formData.append("qr", "1");
    }
    let uploadRes = Axios.post("upload.php", formData, 
    {
      headers: 
      {
        "Content-Type": "multipart/form-data",
      },
      onUploadProgress
    });
    console.log('Api upload qr = ' + qr);
    console.log(uploadRes);    

    return uploadRes;
};

// export const session = async () => 
// {
//     const res = await Axios.get('session.php');
//     console.log("Api session");
//     console.log(res);

//     const {data} = res;
//     return data;
// }

export const logOut = () =>
{
    Axios.post('logout.php')
    .then((res) =>
    {
        console.log('Header logout');
        console.log(res);    
    })
    .catch((err) => console.error(err));
}

// export const sleep = (ms: number) => 
// {
//     return new Promise((resolve) => setTimeout(resolve, ms));
// };
    
