<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Whatsapp extends CI_Controller {
   public function __construct() {

    parent::__construct();
    $this->load->library('whatsapplib');
    $this->load->model('M_whatsapp');
    
  }
  // public function pairing(){
  //   $host = $this->config->item('host');
  //   $token = $this->config->item('token');
  //   $deviceid = $this->config->item('deviceid');
  //   $devicekey = $this->config->item('devicekey');
  //   function get_string_between($string, $start, $end){
  //       $string = ' ' . $string;
  //       $ini = strpos($string, $start);
  //       if ($ini == 0) return '';
  //       $ini += strlen($start);
  //       $len = strpos($string, $end, $ini) - $ini;
  //       return substr($string, $ini, $len);
  //   }
    
  //   $curl = curl_init();
  //   curl_setopt_array($curl, [
  //     CURLOPT_URL => "$host/devices/$deviceid/pair?html=true",
  //     CURLOPT_RETURNTRANSFER => true,
  //     CURLOPT_ENCODING => "",
  //     CURLOPT_MAXREDIRS => 10,
  //     CURLOPT_TIMEOUT => 30,
  //     CURLOPT_SSL_VERIFYHOST => false,
  //     CURLOPT_SSL_VERIFYPEER => false,  
  //     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  //     CURLOPT_CUSTOMREQUEST => "GET",
  //     CURLOPT_POSTFIELDS => "",
  //     CURLOPT_HTTPHEADER => [
  //       "Authorization: Bearer $token",
  //     ],
  //   ]);
  //   $response = curl_exec($curl);
  //   $err = curl_error($curl);
  //   curl_close($curl);

  //   if ($err) {
  //     $resp = array('status' => 'error','message' => $err,);
  //   } else {
  //     $QRCode = get_string_between($response, '<img src="', '" alt="">');
  //     $status = get_string_between($response, '<h3>status : <b>', '</b></h3>');
  //     $message = get_string_between($response, '<h3>message : ', '</h3>');
  //     if ($status == "PAIRED") {
  //       $pair = true;
  //     }else{
  //       $pair = false;
  //     }
  //     //status: PAIRED,PAIRUNG,IDLE
  //     $resp = array('status' => 'success','pair' => $pair, 'status_pair' => $status, 'message' => $message, 'qrcode' => $QRCode,);

  //     echo json_encode($resp); 
  //   }
  // }
  public function pairing(){
    $host = $this->config->item('host');
    $token = $this->config->item('token');
    $aa= $this->device($host,$token);
    $ar = (array)json_decode($aa);
    if($ar['result']=="true"){
      // echo "konek";
      $pair = true;
      $QRCode = "";
      $status = "PAIRED";
    }else{
      $curl = curl_init();

      curl_setopt_array($curl, array(
        CURLOPT_URL => $host.'/api/qrcode',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => 'token='.$token,
      ));

      $response = curl_exec($curl);
      $err = curl_error($curl);
      curl_close($curl);
      // var_dump($err)
      // echo $response;
      // var_dump(json_decode($response));
      $a = (array)json_decode($response);
      // var_dump($a);
      // echo "<img src='".$a['qrcode']."' style='width:200px;height:auto;'>";
      $pair = false;
      $QRCode = $a['qrcode'];
      $status = "UNPAIR";
    }
     $resp = array('status' => 'success','pair' => $pair, 'status_pair' => $status, 'qrcode' => $QRCode,);
     echo json_encode($resp); 
  }
  public function device($host,$token)
  {
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => $host.'/api/device',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => 'token='.$token,
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    // echo $response;
    return $response;
  }
  public function reload()
  {
    $host = $this->config->item('host');
    $token = $this->config->item('token');
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => $host.'/api/reload',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => 'token='.$token,
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    // echo $response;
    $r = (array)json_decode($response);
    if ($r['result'] == "true") {
      $arrayName = array('status' => 200);
    }else{
      $arrayName = array('status' => 500);
    }
    echo json_encode($arrayName);
  }
  // public function unpair(){
  //   $host = $this->config->item('host');
  //   $token = $this->config->item('token');
  //   $deviceid = $this->config->item('deviceid');
  //   $devicekey = $this->config->item('devicekey');
  //   $curl = curl_init();
  //   curl_setopt_array($curl, [
  //     CURLOPT_URL => "$host/devices/$deviceid/unpair?html=true",
  //     CURLOPT_RETURNTRANSFER => true,
  //     CURLOPT_ENCODING => "",
  //     CURLOPT_MAXREDIRS => 10,
  //     CURLOPT_TIMEOUT => 30,
  //     CURLOPT_SSL_VERIFYHOST => false,
  //     CURLOPT_SSL_VERIFYPEER => false,  
  //     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  //     CURLOPT_CUSTOMREQUEST => "GET",
  //     CURLOPT_POSTFIELDS => "",
  //     CURLOPT_HTTPHEADER => [
  //       "Authorization: Bearer $token",
  //     ],
  //   ]);

  //   $response = curl_exec($curl);
  //   $err = curl_error($curl);

  //   curl_close($curl);

  //   if ($err) {
  //     echo "cURL Error #:" . $err;
  //   } else {
  //     // echo $response;
  //     $resp = array('status' => 'success','message' => 'Berhasil Unpair',);
  //     echo json_encode($resp);
  //   }
  // }

  public function unpair(){
    $host = $this->config->item('host');
    $token = $this->config->item('token');
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => $host.'/api/unpair',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => 'token='.$token,
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    // echo $response;
    $arres = (array)json_decode($response);
    if ($arres == "false") {
      echo "cURL error";
    }else{
      
      $resp = array('status' => 'success','message' => 'Berhasil Unpair',);
      echo json_encode($resp);
    }
    
  }
  public function send()
  {
    $token = "62zbM3LzuWRSmr5zkSAHorkyABiHG6dyFimq6er6GB33MT4K1L";
    $phone= "6282212133247"; //untuk group pakai groupid contoh: 62812xxxxxx-xxxxx
    $message = "Testing by API ruangwa";

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://app.ruangwa.id/api/send_message',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => 'token='.$token.'&number='.$phone.'&message='.$message,
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
  }
  // public function send_text(){
    
  //   $this->whatsapplib->sendwa("6283892422246","📌berhasil");
  // }
  public function data_unit_misioner(){
        $result = $this->M_whatsapp->data_unit_misioner()->result_array();
        echo json_encode($result);
  }
  public function data_users(){
        $result = $this->M_whatsapp->data_users()->result_array();
        echo json_encode($result);
  }
  public function send_um(){
    $msg                = $this->input->post('msg',TRUE);
    $um                = $this->input->post('um',TRUE);
    if ($um == "all") {
      $result = $this->M_whatsapp->data_user_all()->result_array();
      foreach ($result as $key) {
        $this->whatsapplib->sendwa($key['tlp'],$msg);
      }
      echo true;
    }else{
      $arr = explode (",",$um);
      $data['um']=$arr[0];
      $result = $this->M_whatsapp->data_user_um($data)->result_array();
      foreach ($result as $key) {
        $this->whatsapplib->sendwa($key['tlp'],$msg);
      }
      echo true;
    }
    
  }
  public function send_user(){
    $msg                = $this->input->post('msg',TRUE);
    $user                = $this->input->post('user',TRUE);
    $this->whatsapplib->sendwa($user,$msg);
    echo true;
  }
    
}
