import { Injectable } from '@angular/core';
import { environment } from '../environments/environment';
import { HttpClient, HttpHeaders, HttpResponse, HttpClientModule } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class CommonFunctionService {

  projectpath = environment.projectpath;


  constructor(private http: HttpClient) {
  }




  getAllData(url: string) {
    var headers = new HttpHeaders();
    console.log("url---->", url);
    headers = headers.append("Content-Type", "application/json")
    headers = headers.append("Access-Control-Allow-Origin", '*')
    let text = "Mr. Blue has a blue house";
    // let position = text.search("blue");
    var urlfind = url.includes("loginuser") || url.includes("changepassword");
    if (urlfind) {
      console.log("login url");
      let abc: any;
      abc = this.http.get(url, { headers: headers });
      return abc;

    } else {
      console.log("other url");
      var gettoken = localStorage.getItem("tokenkey");

      console.log("url---->", url);
      headers = headers.append("token", gettoken);

      console.log(headers.get('token'));

      //headers.append('token', gettoken);
      console.log("headers", { headers: headers })
      let abc: any;
      abc = this.http.get(url, { headers: headers });
      return abc;

    }


  }

  getData(url: string, json: any) {
    console.log("params=======>", json);
    console.log("url---->", url)
    var params = json;
    var headers = new HttpHeaders();
    headers = headers.append("Content-Type", "application/json")
    headers = headers.append("Access-Control-Allow-Origin", '*')

    var urlfind = url.includes("loginuser") || url.includes("changepassword");
    if (urlfind) {
      console.log("login url");
      let abc: any; abc = this.http.post(url, params,
        { headers: headers });
      return abc;

    } else {
      console.log("other url");
      var gettoken = localStorage.getItem("tokenkey");


      console.log("url---->", url);
      headers = headers.append("token", gettoken);
      //console.log(headers.get('token'));
      let abc: any; abc = this.http.post(url, params,
        { headers: headers });
      return abc


    }
    ////console.log*("params:", JSON.stringify(params));

  }


  updateData(url: string, json: any) {
    console.log("params=======>", json);
    console.log("url---->", url)
    var params = json;
    var headers = new HttpHeaders();
    headers = headers.append("Content-Type", "application/json")
    headers = headers.append("Access-Control-Allow-Origin", '*')

    var urlfind = url.includes("loginuser") || url.includes("changepassword");
    if (urlfind) {
      console.log("login url");
      let abc: any; abc = this.http.post(url, params,
        { headers: headers });
      return abc;

    } else {
      console.log("other url");
      var gettoken = localStorage.getItem("tokenkey");


      console.log("url---->", url);
      headers = headers.append("token", gettoken);
      //console.log(headers.get('token'));
      let abc: any; abc = this.http.put(url, params,
        { headers: headers });
      return abc


    }
    ////console.log*("params:", JSON.stringify(params));

  }

  updateContact(url: string, json: any) {
        var headers = new HttpHeaders();
    console.log("url---->", url);
    headers = headers.append("Content-Type", "application/json")
    headers = headers.append("Access-Control-Allow-Origin", '*')

    var urlfind = url.includes("loginuser") || url.includes("changepassword");
    if (urlfind) {
      console.log("login url");
      let abc: any;
      abc = this.http.put(url, json, { headers: headers });

      return abc;

    } else {
      console.log("other url");
      var gettoken = localStorage.getItem("tokenkey");

      console.log("url---->", url);
      headers = headers.append("token", gettoken);

      console.log(headers.get('token'));

      //headers.append('token', gettoken);
      console.log("headers", { headers: headers })
      let abc: any;
      abc = this.http.put(url, json, { headers: headers });

      return abc;

    }
  }

  deleteContact(url: string, login_id: string, contact_id: any) {
    var headers = new HttpHeaders();
    console.log("url---->", url);
    headers = headers.append("Content-Type", "application/json")
    headers = headers.append("Access-Control-Allow-Origin", '*')

    var urlfind = url.includes("loginuser") || url.includes("changepassword");
    if (urlfind) {
      console.log("login url");
      let abc: any;
      abc = this.http.delete(url, { headers: headers, 
        body: { updated_by: login_id, contact_id: contact_id } 
      });

      return abc;

    } else {
      console.log("other url");
      var gettoken = localStorage.getItem("tokenkey");

      console.log("url---->", url);
      headers = headers.append("token", gettoken);

      console.log(headers.get('token'));

      //headers.append('token', gettoken);
      console.log("headers", { headers: headers })
      let abc: any;
      abc = this.http.delete(url, { headers: headers, 
        body: { updated_by: login_id, contact_id: contact_id } 
      });

      return abc;
    }
  }

}