<?php

use system\library\feedback\gupshop\MessageGupshopApi;
use system\library\feedback\gupshop\MessageGupshopText;

class WhatsappOrderStatus extends Whatsapp
{

    public function __construct($registry)
    {
        parent::__construct($registry);
    }

    public function sendWhats($typewhats, $order_id, $attribute_value)
    {
        $attribute_value = json_decode($attribute_value, true);
        $feedback_message_status = $this->config->get('feedback_message_status');

        $keyIndex = array_search($attribute_value['order_status_id'], array_column($feedback_message_status, 'id'));

        $message = 0;
        if(trim($feedback_message_status[$keyIndex]['message'])!=''){
            $message = $feedback_message_status[$keyIndex]['message'];
        }else{
            $keyAll = array_search(Type_FeedbackOrderStatus::Tp_AllStatus, array_column($feedback_message_status, 'id'));
            if($feedback_message_status[$keyAll]['message']){
                $message = $feedback_message_status[$keyAll]['message'];
            }
        }

        if($message){

            try{

                $this->db->startTransaction();

                $this->getCustomer($attribute_value['customer_id']);
                $this->getOrder($order_id);

                $variable = array(
                    Type_LayoutPersonalizadoVarButton::Tp_Clinome => Type_LayoutPersonalizadoVarButton::getNickFromValue(Type_LayoutPersonalizadoVarButton::Tp_Clinome),
                    Type_LayoutPersonalizadoVarButton::Tp_Cliemail => Type_LayoutPersonalizadoVarButton::getNickFromValue(Type_LayoutPersonalizadoVarButton::Tp_Cliemail),
                    Type_LayoutPersonalizadoVarButton::Tp_Lojnome => Type_LayoutPersonalizadoVarButton::getNickFromValue(Type_LayoutPersonalizadoVarButton::Tp_Lojnome),
                    Type_LayoutPersonalizadoVarButton::Tp_Lojlink => Type_LayoutPersonalizadoVarButton::getNickFromValue(Type_LayoutPersonalizadoVarButton::Tp_Lojlink),
                    Type_LayoutPersonalizadoVarButton::Tp_Lojloginlink => Type_LayoutPersonalizadoVarButton::getNickFromValue(Type_LayoutPersonalizadoVarButton::Tp_Lojloginlink),
                    Type_LayoutPersonalizadoVarButton::Tp_PedidoID => Type_LayoutPersonalizadoVarButton::getNickFromValue(Type_LayoutPersonalizadoVarButton::Tp_PedidoID),
                );

                $message = $this->ftrgeralregistry->formatMessageVariable($message, $this->getValuesVariable(), $variable);

                $gupshopApi = new MessageGupshopApi();
                $gupshopApi->setDestination($attribute_value['feedback_order_whatsapp']);
                $gupshopText = new MessageGupshopText();
                $gupshopText->setIsHSM('true');
                $gupshopText->setType(Type_FeedbackMessage::Tp_Text);
                $gupshopText->setText($message);
                $gupshopApi->setMessage($gupshopText);

                $this->log->baseLog->setTipoEnviado(Type_LogEnviado::Tp_LogEnviado);
                $this->log->baseLog->setTipoIntegracao(Type_LogIntegracao::Tp_Gupshop);
                $this->log->baseLog->setOrigem(__METHOD__);
                $this->log->baseLog->setTipoLog(Type_Log::Tp_LogPedido);
                $this->log->baseLog->setTipoId(Type_LogId::Tp_LogIdPedido);
                $this->log->baseLog->setEntidadeId($order_id);

                $result = $this->feedback->sendMessages($this->feedback->getIntegrationEnabled(), $gupshopApi, $this->log);

                if($result){

                    if(property_exists($result, 'type') && ($result->type == 'error')){
                        throw new Exception($result->message);
                    }

                    if(property_exists($result, 'status') && ($result->status == 'submitted')){
                        $this->load->dmodel('sale/order');
                        $data = array(
                            'order_history_id' => $attribute_value['order_history_id'],
                            'notify_whatsapp' => Type_Status::Tp_Habilitado,
                            'message_whatsapp' => $message
                        );
                        $this->dmodel_sale_order->setOrderHistoryWhatsapp($data);
                    }

                }

                $this->db->commit();

            } catch (Exception $e) {
                $this->db->rollback();
                throw $e;
            }

        }
    }


}