<?php

namespace App\Libraries\MyCloudflare;

use Cloudflare\API\Adapter\Guzzle;
use App\Models\Cloudflare\AccountModel;
use App\Libraries\MyCloudflare\MyCloudflare;
use App\Entities\Cloudflare\AccountEntity;

class Account extends MyCloudflare
{
    private $_myStorage = null;
    private $_authkey = "";
    public function __construct()
    {
        parent::__construct();
    }
    private function getRequest(): Guzzle
    {
        return $this->getMySocket()->request($this->_authkey);
    }
    final protected function getMyStorage(): AccountModel
    {
        if ($this->_myStorage === null) {
            $this->_myStorage = new AccountModel();
        }
        return $this->_myStorage;
    }
    //Result 형태
    // [
    //   {"id":"078e88a7735965b661715af13031ecb0",
    //   "name":"Cloudwin002@idcjp.jp's Auth",
    //   "type":"standard",
    //   "settings":{
    //     "enforce_twofactor":false,
    //     "api_access_enabled":null,
    //     "access_approval_expiry":null,
    //     "use_account_custom_ns_by_default":false
    //   },
    //   "legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}},
    //   "created_on":"2017-06-26T05:44:49.470184Z"}
    // ]
    protected function getArrayByResult($result, array $formDatas = []): array
    {
        $formDatas[$this->getMyStorage()->getPKField()]    = $result->id;
        $formDatas[$this->getMyStorage()->getTitleField()] = $result->name;
        $formDatas['type']                                 = $result->type;
        $formDatas['status']                               = 'use';
        $formDatas['updated_at']                           = $result->created_on;
        $formDatas['created_at']                           = $result->created_on;
        return $formDatas;
    }
    // public function create(array $formDatas): AccountEntity
    // {
    //     //Socket용
    //     $cf = $this->getMySocket()->request($formDatas['authkey'])
    //         ->post('accounts', [
    //             'name' => $formDatas[$this->getMyStorage()->getTitleField()],
    //             'type' => $formDatas['type'],
    //         ]);
    //     $cf = json_decode($cf->getBody());
    //     if (!$cf->success) {
    //         throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true));
    //     }
    //     //Storage용
    //     $formDatas = $this->getArrayByResult($cf->result, $formDatas);
    //     $entity    = $this->getMyStorage()->create($formDatas);
    //     log_message("notice", "Account::" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
    //     return $entity;
    // }
    public function create(array $formDatas): array
    {
        //Account의 경우 인증키를 가지고있고 생성할수 없으므로 get으로 읽은후 첫번째것을 사용하기로 함
        $entitys = [];
        foreach ($this->reload_cfs($this->getRequest(), 'accounts') as $cf) {
            $formDatas = $this->getArrayByResult($cf->result);
            $entity    = $this->$this->getMyStorage()->create($formDatas);
            log_message("notice", "Account:" . __FUNCTION__ . "=> {$entity->getTitle()} 작업을 완료하였습니다.");
            $entitys[] = $entity;
        }
        return $entitys;
    }

    protected function reload_entity($cf): AccountEntity
    {
        return $this->getMyStorage()->modify(new AccountEntity, $this->getArrayByResult($cf));
    }
}
