import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
//import 'rxjs/add/operator/map';
import { environment } from 'src/environments/environment';
import { SessionDataProviderService } from '../store/session-data-provider.service';
import {Router} from "@angular/router";


@Injectable({
  providedIn: 'root'
})
export class WrenchService {
  apiKey = '99dfe35fcb7de1ee';
  url;
  
  constructor(private http: HttpClient,
              private router: Router,
              public sessionDataProviderService :SessionDataProviderService) {
      // this is test
    this.url = 'https://apigate.lotus.g1.wrenchboard.com/en/wrench/api/v1';
   }
   getPostData(reqPath:string, reqData): Observable<any> {

      if (reqPath != 'userlogin' && reqPath != 'createuser'
          && reqPath !='stepresetpass' && reqPath !='startresetpasword' && reqPath!='qrlogin'){
          const sessionId =  this.sessionDataProviderService.session;
          if (sessionId.length==0){
              //debugger;
              this.router.navigate(['login']);
          }
      }
    const apiReturn =  this.http.post(
      `${environment.baseUrl}/${reqPath}`,reqData
    );
      /*
      We have to check if this sessio
      {"status_msg":"invalid_session_msg","internal_return":"-9999"}
       */
       // if (apiReturn.internal_return == -9999){
       //
       // }
/*
       apiReturn.pipe().subscribe(ret => {
           //alert('Todays report: ' + internal_return);

           if (ret["internal_return"] == -9999){
               console.log("AMEYE**** ",ret["internal_return"]);
               this.sessionDataProviderService.DestroySessionOnLogout();
               this.router.navigate(['login']);
           }

       });
*/

      return apiReturn;
  }

  startPassReset(reqData){
      return this.getPostData('startresetpasword', reqData);
      //
  }

    getManagerOffersList(reqData){
        return this.getPostData('offersinterestlist', reqData);
    }
   getJobManagerList(reqData){
        return this.getPostData('jobmanagerlist', reqData);
    }

    verifyPassReset(reqData){
        return this.getPostData('stepresetpass', reqData);
        //
    }
    createUser(reqData) {
        return this.getPostData('createuser', reqData);
    }
    completeMobileSignup(reqData) {
        return this.getPostData('completemobileuser', reqData);
    }

    referHx(reqData) {
        return this.getPostData('refferhx', reqData);
    }

    sendRefer(reqData) {
        return this.getPostData('sendreferral', reqData);
    }

    offersInterestProcces(reqData){
        return this.getPostData('offersinterestproc', reqData);
    }

//

    signUpCountry(apiData) {
      /*
      array (size=3)
  'limit' => int 10
  'page' => string '1' (length=1)
  'action' => int 1200021
       */
        return this.getPostData('signupcountry',apiData);
    }
    getMyNotifications(usrData) {
        return this.getPostData('mynotifications',usrData);
    }

    getMyPlayground(usrData) {
        return this.getPostData('playground',usrData);
    }

    loginUser(loginData) {
    return this.getPostData('userlogin',loginData);  
  }

  authStart(authData) {
    return this.getPostData("authstart", authData);
  }

  qrLoginUser(loginData) {
    return this.getPostData('qrlogin',loginData);
  }

  getBlogData(blogReq){
    //const blogReq=[];
    return this.getPostData('blogdata',blogReq);  
    //blogdata
  }

  getWaitingInterest(usrData){
      return this.getPostData('waitinginterest',usrData);
  }

  getusersCardList(usrData){
      return this.getPostData('paylistcard',usrData);
  }

  deleteUserCard(reqData){
      return this.getPostData("payremcard", reqData);
  }
    setSuggestStatus(usrData){
        return this.getPostData('suggeststatus',usrData);
    }
  //recipients
  getHomeBanners(usrData){
      return this.getPostData('homebanners',usrData);
  }

  //

    getFamilySampleTasks(reqData) {
        return this.getPostData("familysampletasks", reqData);
    }
    getFamilyList(usrData) {
        return this.getPostData("familylist", usrData);
    }
    getFamilyPeningList(usrData) {
        return this.getPostData("familywaitingtasks", usrData);
    }
    getUserWallets(usrData) {
        return this.getPostData("getwallets", usrData);
    }


    getFamilyWallets(reqData) {
        return this.getPostData("familywallet", reqData);
    }

    getPaymentHx(usrData) {
        return this.getPostData("paymenthx", usrData);
    }
    getJobsData(usrData) {
        return this.getPostData("getjobsdata", usrData);
    }

    getJobsManagerData(usrData) {
        return this.getPostData("jobmanageractive", usrData);
    }
    sendMarketInterest(reqData){
        return this.getPostData("marketinterest", reqData);
    }
    sendMarketMessage(reqData){
        return this.getPostData("marketmessage", reqData);
    }
    getMyActiveJobsData(usrData) {
        return this.getPostData("activetaskslist", usrData);
    }

    addRecipientAccount(reqData){
        return this.getPostData("addrecipient", reqData);
    }

    accountTypes(reqData){
        return this.getPostData("accounttypes", reqData);
    }
    countryBanks(reqData){
        return this.getPostData("countrybanks", reqData);
    }
    getMyFiles(usrData){
        return this.getPostData("myfiles", usrData);
    }

    getMyOffers(usrData){
        return this.getPostData("offerslist", usrData);
    }

    requestDelete(usrData){
        return this.getPostData("reqdel", usrData);
    }

    updateProfile(profileData) {
        return this.getPostData("updateprofile", profileData);
    }

    offferResponse(respData){
        return this.getPostData("offersresponse", respData);
    }

    sendTaskMessage(messageData){
        return this.getPostData("sendtaskmessage", messageData);
    }

    activeJobMsgList(contractData){
        return this.getPostData("activejobmsglist", contractData);
    }

    profilePassChange(reqData){
        return this.getPostData("profilepasschange", reqData);
    }

    jobmanageroffers(usrData){
        return this.getPostData("jobmanageroffers", usrData);
    }

    recipientAccount(usrData){
        return this.getPostData("recipients", usrData);
    }

    sendMoneyFee(usrData){
        return this.getPostData("sendmoneyfee", usrData);
    }

    getCouponHx(usrData){
        return this.getPostData("couponhx", usrData);
    }

    getCouponPending(usrData){
        return this.getPostData("couponpending", usrData);
    }
    redeemCoupon(redeemData){
        return this.getPostData("couponredeem", redeemData);
    }
    sendMoneyAction(sendmoneyData){
        return this.getPostData("sendmoney", sendmoneyData);
    }

    familyStartTransfer(reqData){
        return this.getPostData("familytransferstart", reqData);
    }

    familyTransfer(reqData){
        return this.getPostData("familytransfer", reqData);
    }

    familySuggestList(usrData){
        return this.getPostData("familysuggestlist", usrData);
    }
    // END POINT FOR WORKER TO MARK TASK AS COMPLETED
    workerJobAction(reqData) {
        var postData = {
            uid: this.sessionDataProviderService.member_uid,
            member_id:  this.sessionDataProviderService.member_id,
            sessionid:  this.sessionDataProviderService.session,
            action: 14015,
            ...reqData,
        };
        return this.getPostData("/activetaskstatus", postData);
    }

    familyManage(postData){
        return this.getPostData("/familymanage", postData);
    }

    uploadFile(uploadData){
        return this.getPostData("/uploads", uploadData);
    }

  /*




  registerUser(newUserData) {
    // no session needed - will start the session
    return this.http.post(this.url + '/createmobileuser', newUserData)
      .map(res => res.json());
  }
  completeRegisterUser(newUserData) {
    // no session needed - will start the session
    return this.http.post(this.url + '/completemobileuser', newUserData)
      .map(res => res.json());
  }

  startResetPassword(UserData) {
    // no session needed - will start the session
    return this.http.post(this.url + '/startresetpasword', UserData)
      .map(res => res.json());
  }

  getStartData() {
    return this.http.post(this.url + '/startjoblist', { limit: '10', page: '1' })
      .map(res => res.json());
  }

  getJobsData() {
    return this.http.post(this.url + '/getjobsdata', { limit: '20', page: '1' })
      .map(res => res.json());
  }

  getPendingJobsData(userData: any) {
    return this.http.post(this.url + '/getpendingjobs', userData)
      .map(res => res.json());
  }

  getMessages(UserData) {
    console.log("getMessages Called");
    return this.http.post(this.url + '/message', UserData)
      .map(res => res.json());
  }
  //JOBS
  getActiveJobData(UserData) {
    console.log("getActiveJobData Called");
    return this.http.post(this.url + '/activejoblist', UserData)
      .map(res => res.json());
  }

  getMyOffersJobData(UserData) {
    console.log("getMyOffersJobData Called");
    return this.http.post(this.url + '/offerslist', UserData)
      .map(res => res.json());
  }
  //=========================================
  getPaymentHx(UserData) {
    console.log("getPaymentHx Called");
    return this.http.post(this.url + '/paymenthx', UserData)
      .map(res => res.json());
  }
  
  getOffersJobDataff() {
    console.log("getOffersJobData Called");
    return this.http.post(this.url + '/offerslist', { limit: '10', page: '1', sessionid: 'ADNGHGHGHGHGHGH', member_id: 1 })
      .map(res => res.json());
  }
  
  getDashData(memberID, sessionID) {
    // session required here 
    return this.http.post(this.url + '/dashdata', { sessionid: sessionID, member_id: memberID })
      .map(res => res.json());
  }
  getUserAccount(memberID, sessionID) {
    // account
    return this.http.post(this.url + '/account', { sessionid: sessionID, member_id: memberID })
      .map(res => res.json());
  }
  getProfile(memberID, sessionID) {
    //loadprofile
    return this.http.post(this.url + '/loadprofile', { sessionid: sessionID, member_id: memberID })
      .map(res => res.json());
  }

  getUserBankList(UserData) {
    console.log("getActiveJobData Called");
    return this.http.post(this.url + '/mybanklist', UserData)
      .map(res => res.json());
  }
  userSendMoneyt(SendMoneydata) {
    console.log("userSendMoneyt Called");
    return this.http.post(this.url + '/sendmoney', SendMoneydata)
      .map(res => res.json());
  }

  sendMoneyFee(SendMoneydata) {
    console.log("sendMoneyFee Called");
    return this.http.post(this.url + '/sendmoneyfee', SendMoneydata)
      .map(res => res.json());
  }
  sendJobInterest(SendJobInterestData) {
    console.log("sendJobInterest Called");
    return this.http.post(this.url + '/sendinterest', SendJobInterestData)
      .map(res => res.json());
  }
  sendOfferInterest(SendJobInterestData) {
    console.log("sendOfferInterest Called");
    return this.http.post(this.url + '/sendofferinterest', SendJobInterestData)
      .map(res => res.json());
  }

  sendJobInterestMessage(sendJobInterestMessage) {
    console.log("sendJobInterestMessage Called");
    return this.http.post(this.url + '/sendinterestmessage', sendJobInterestMessage)
      .map(res => res.json());
  }

  getTaskMessage(taskData: any) {
    console.log("getTaskMessage Called");
    return this.http.post(this.url + '/taskmessage', taskData)
      .map(res => res.json());
  }

  sendTaskMessage(taskMessage: any) {
    console.log("sendTaskMessage Called");
    return this.http.post(this.url + '/sendtaskmessage', taskMessage)
      .map(res => res.json());
  }
  */
}
