import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { URL } from '../mocks/url';

@Injectable({
	providedIn: 'root'
})
export class OrderService {

	constructor(
		private http: HttpClient
	) { }

	async createOrder(userId: any, userCart: any) {
        let cart: any = userCart;

        cart.app_user = {
            user_id: userId
        };

	    cart.orderstatus = {
            orderstatus_id: 6
        };

        cart.soft_delete = false;

		const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.post(URL + '/api/maioraservice/orders/v1/create/', cart, httpOptions).toPromise();
	}

	async getOrders(id: any) {
		const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.get(URL + '/api/maioraservice/orders/getorders/user_id/' + id, httpOptions).toPromise();
	}

    async generateInvoice(data: any) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.post(URL + '/Payment/v1/invoice/create', data, httpOptions).toPromise();
	}
	
	async getPromoCodes(mallId: number) {
		const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.get(URL + '/api/maioraservice/offers/getoffer/mall_id/' + mallId, httpOptions).toPromise();
	}

	async getPaymentDetailsByOrderID(orderId: string) {
		const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.get(URL + '/Payment/v1/Order/order_id/' + orderId, httpOptions).toPromise();
    }
    
    async getAllOrderStatus() {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.get(URL + '/api/maioraservice/orders/getallOrderStatus', httpOptions).toPromise();
    }
    

    async updateOrder(order: any) {
		const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.put(URL + '/api/maioraservice/orders/v1/update', order ,httpOptions).toPromise();
	}
}
