<?php

namespace App\Controllers\Panel\Whatsapp;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;

class Device extends BC
{
    use ResponseTrait;
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $data['page_title'] = "Whatsapp - Perangkat";
        return view('Panel/Whatsapp/device', $data);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $status = $this->request->getVar('status');
        $is_connected = $this->request->getVar('is_connected');
        $is_default = $this->request->getVar('is_default');
        $db = db_connect();
        $devices = $db->table('whatsapp_device');
        if (in_array($status, ['0', '1'])) $devices->where('status', $status);
        if (in_array($is_connected, ['0', '1'])) $devices->where('is_connected', $is_connected);
        if (in_array($is_default, ['0', '1'])) $devices->where('is_default', $is_default);
        $devices = $devices->get()->getResultArray();
        return $this->respond(['data' => $devices]);
    }
    public function detail($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = db_connect()->table('whatsapp_device')->getWhere(['id' => $id])->getRowArray();
        if (!$data) return $this->failNotFound("Perangkat tidak ditemukan");
        return $this->respond($data);
    }

    public function add()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $device_id = $this->request->getVar('device_id');
        $deviceInfo  = getWADeviceInfo($device_id);
        $db = db_connect();
        if ($db->table('whatsapp_device')->getWhere(['device_id' => $device_id])->getRowArray()) {
            return $this->fail("Perangkat dengan ID \n \"$device_id\" sudah ada");
        }
        if (!$deviceInfo['status']) return $this->fail("Gagal mendapatkan info : \"$deviceInfo[message]\"");
        $data['nomor'] = $deviceInfo['data']['nomor'];
        $data['nama'] = empty($this->request->getVar('nama')) && $deviceInfo['data']['nama'] ? $deviceInfo['data']['nama'] : $this->request->getVar('nama');
        $data['device_id']  = $device_id;
        $data['is_default'] = $this->request->getVar('is_default');
        $data['updated_at'] = time();
        $data['created_at'] = time();
        $data['is_connected'] =  $deviceInfo['data']['status'] == "CONNECTED" ? '1' : '0';
        $data['status'] = $data['is_connected'] == '0' ? '0' : $this->request->getVar('status');
        if (empty($data['nama'])) return $this->fail("Nama Perangkat tidak boleh kosong");
        $db->transStart();
        if ($data['is_default'] == '1') $db->table('whatsapp_device')->update(['is_default' => '0']);
        $db->table('whatsapp_device')->insert($data);
        $db->transComplete();
        if ($db->transStatus() === FALSE) return $this->fail("Gagal menambah perangkat");
        return $this->respond(['message' => "Berhasil menambah perangkat"]);
    }
    public function edit($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $db = db_connect();
        $device = $db->table('whatsapp_device')->getWhere(['id' => $id])->getRowArray();
        if (!$device) return $this->failNotFound('Data tidak ditemukan');
        $deviceInfo = getWADeviceInfo($device['device_id']);
        if ($deviceInfo['status']) {
            $data['nomor'] = $deviceInfo['data']['nomor'];
            $data['nama'] = empty($this->request->getVar('nama')) && $deviceInfo['data']['nama'] ? $deviceInfo['data']['nama'] : $this->request->getVar('nama');
            $data['is_connected'] = $deviceInfo['data']['status'] == "CONNECTED" ? '1' : '0';
            $data['status'] = $data['is_connected'] == '0' ? '0' : $this->request->getVar('status');
        } else {
            $data['nama'] = $this->request->getVar('nama');
            $data['status'] = $device['is_connected'] == '0' ? '0' : $this->request->getVar('status');
        }
        $data['updated_at'] = time();
        $data['is_default'] = $this->request->getVar('is_default');
        if (empty($data['nama'])) return $this->fail("Nama Perangkat tidak boleh kosong");
        $db->transStart();
        if ($data['is_default'] == '1') $db->table('whatsapp_device')->update(['is_default' => '0'], ['id !=' => $id]);
        $db->table('whatsapp_device')->update($data, ['id' => $id]);
        $db->transComplete();
        if ($db->transStatus() === FALSE) return $this->fail("Gagal mengubah perangkat");
        return $this->respond(['message' => "Berhasil mengubah perangkat"]);
    }
    public function delete($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }

        $db =  db_connect();
        $db->table('whatsapp_device')->delete(['id' => $id]);
        if ($db->affectedRows() <= 0) return $this->fail("Gagal menghapus perangkat");
        return $this->respond(['message' => "Berhasil menghapus perangkat"]);
    }
}
