<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;

use backend\models\SignupForm;
use backend\models\ChangePasswordForm;
use backend\models\SetPasswordForm;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            // 'access' => [
            //     'class' => AccessControl::className(),
            //     'rules' => [
            //         [
            //             'actions' => ['login', 'error'],
            //             'allow' => true,
            //         ],
            //         [
            //             'actions' => ['logout', 'index'],
            //             'allow' => true,
            //             'roles' => ['@'],
            //         ],
            //     ],
            // ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionHelp(){
        return $this->render('help');
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionSignup() {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                Yii::$app->session->setFlash('success', 'User berhasil dibuat.');
                return $this->goHome();
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }

    public function actionChangePassword() {
        $model = new ChangePasswordForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->changePassword()) {
                Yii::$app->session->setFlash('success', 'Password berhasil diubah.');
                return $this->goHome();
            }
        }

        return $this->render('change-password', [
            'model' => $model,
        ]);
    }

    public function actionSetPassword() {
        $model = new SetPasswordForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->setPassword()) {
                Yii::$app->session->setFlash('success', 'Password berhasil diubah.');
                return $this->goHome();
            }
        }

        return $this->render('set-password', [
            'model' => $model,
        ]);
    }

    public function actionSetDb() {
        $file = '../../common/config/main-local.php';
        $fileContent = file_get_contents($file);
        $dbhost = $_POST['dbhost'];
        $dbname = $_POST['dbname'];
        $dbusername = $_POST['dbusername'];
        $dbpassword = $_POST['dbpassword'];

        $dsn = explode("'dsn' => ", $fileContent);
        $dsn = explode(",", $dsn[1]);
        $dsn = $dsn[0];
        $dsn = trim($dsn, "'");
        $newDsn = "mysql:host=$dbhost;dbname=$dbname";

        $username = explode("'username' => ", $fileContent);
        $username = explode(",", $username[1]);
        $username = $username[0];
        $username = trim($username, "'");

        $password = explode("'password' => ", $fileContent);
        $password = explode(",", $password[1]);
        $password = $password[0];
        $password = trim($password, "'");

        $fileContent = str_replace("'dsn' => '$dsn'", "'dsn' => '$newDsn'", $fileContent);
        $fileContent = str_replace("'username' => '$username'", "'username' => '$dbusername'", $fileContent);
        $fileContent = str_replace("'password' => '$password'", "'password' => '$dbpassword'", $fileContent);

        file_put_contents($file, $fileContent);
    }
}
