<?php

namespace App\Controller\Admin;

use App\Controller\Admin\AppAdminController;
use Cake\Network\Exception\NotFoundException;

class OptionsController extends AppAdminController
{
    public function index()
    {
        if ($this->saveOptions()) {
            emptyCache();

            $this->Flash->success(__('Settings have been saved.'));
            return $this->redirect(['action' => 'index']);
        }
    }

    public function email()
    {
        if ($this->saveOptions()) {
            $this->createEmailFile();

            $this->Flash->success(__('Email settings have been saved.'));
            return $this->redirect(['action' => 'email']);
        }
    }

    public function socialLogin()
    {
        if ($this->saveOptions()) {
            $this->Flash->success(__('Social login settings have been saved.'));
            return $this->redirect(['action' => 'socialLogin']);
        }
    }

    public function payment()
    {
        if ($this->saveOptions()) {
            $this->Flash->success(__('Payment settings have been saved.'));
            return $this->redirect(['action' => 'payment']);
        }
    }

    protected function saveOptions()
    {
        $options = $this->Options->find()->all();

        $settings = [];
        foreach ($options as $option) {
            $settings[$option->name] = [
                'id' => $option->id,
                'value' => $option->value
            ];
        }

        if ($this->request->is(['post', 'put'])) {
            foreach ($this->request->data['Options'] as $key => $optionData) {
                if (is_array($optionData['value'])) {
                    $optionData['value'] = serialize($optionData['value']);
                }
                $option = $this->Options->newEntity();
                $option->id = $key;
                $option = $this->Options->patchEntity($option, $optionData);
                $this->Options->save($option);
            }
            return true;
        }

        $this->set('options', $options);
        $this->set('settings', $settings);
    }

    protected function createEmailFile()
    {
        $options = $this->Options->find()->all();

        $config = array(
            'site_name' => '',
            'email_from' => '',
            'email_method' => '',
            'email_smtp_host' => '',
            'email_smtp_port' => '',
            'email_smtp_username' => '',
            'email_smtp_password' => '',
            'email_smtp_tls' => ''
        );

        foreach ($options as $value) {
            if (array_key_exists($value['name'], $config)) {
                $config[$value['name']] = str_replace('\'', '\\\'', $value['value']);
            }
        }

        $result = copy(CONFIG . 'email.install', CONFIG . 'email.php');
        if (!$result) {
            $this->Flash->error(__('Could not copy email.php file.'));
            return false;
        }
        $file = new \Cake\Filesystem\File(CONFIG . 'email.php');
        $content = $file->read();

        foreach ($config as $configKey => $configValue) {
            $content = str_replace('{' . $configKey . '}', $configValue, $content);
        }

        if (!$file->write($content)) {
            $this->Flash->error(__('Could not write email.php file.'));
            return false;
        }
        return true;
    }
}
