<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\admin\channel;

use app\service\admin\BaseAdminService;
use app\service\domain\channel\CoreBottomNavService;
use app\service\domain\channel\CoreChannelService;
use app\service\domain\channel\CoreChannelMessageService;
use app\service\domain\notification\CoreNotificationCenterService;
use think\Request;


/**
 * Channel Service
 * @package app\service\admin\channel
 */
class ChannelService extends BaseAdminService
{

    /**
     * 获取渠道列表
     * @return array
     */
    public function list(): array
    {
        return ['code' => 0, 'data' => $this->core()->list()];
    }


    /**
     * 获取渠道详情
     * @param string $channel 渠道键
     * @return array
     */
    public function detail(string $channel): array
    {
        $detail = $this->core()->detail($channel);
        if (!$detail) {
            return ['code' => 1, 'msg' => ''];
        }
        return ['code' => 0, 'data' => $detail];
    }


    /**
     * 获取底部导航配置
     * @param Request $request 请求对象
     * @return array
     */
    public function bottomNavConfig(Request $request): array
    {
        return ['code' => 0, 'data' => $this->bottomNav()->adminConfig()];
    }


    /**
     * 保存底部导航配置
     * @param Request $request 请求对象
     * @return array
     */
    public function saveBottomNavConfig(Request $request): array
    {
        $data = $request->post('data');
        if (!is_array($data)) {
            return ['code' => 1, 'msg' => ''];
        }

        return $this->bottomNav()->saveConfig($data);
    }


    /**
     * 保存渠道基本信息
     * @param string $channel 渠道键
     * @param Request $request 请求对象
     * @return array
     */
    public function saveBase(string $channel, Request $request): array
    {
        $payload = $request->post('data');
        if (!is_array($payload)) {
            return ['code' => 1, 'msg' => ''];
        }

        $profile = is_array($payload['profile'] ?? null) ? $payload['profile'] : [];
        $config = is_array($payload['config'] ?? null) ? $payload['config'] : [];

        return $this->core()->saveBase($channel, $profile, $config);
    }


    /**
     * 保存渠道支付配置
     * @param string $channel 渠道键
     * @param Request $request 请求对象
     * @return array
     */
    public function savePayments(string $channel, Request $request): array
    {
        $items = $request->post('items');
        if (!is_array($items)) {
            return ['code' => 1, 'msg' => ''];
        }
        return $this->core()->savePayments($channel, $items);
    }


    /**
     * 保存渠道通知配置
     * @param string $channel 渠道键
     * @param Request $request 请求对象
     * @return array
     */
    public function saveNotifies(string $channel, Request $request): array
    {
        $items = $request->post('items');
        if (!is_array($items)) {
            return ['code' => 1, 'msg' => ''];
        }
        return $this->core()->saveNotifies($channel, $items);
    }


    /**
     * 获取渠道模板选项
     * @param string $channel 渠道键
     * @param Request $request 请求对象
     * @return array
     */
    public function templateOptions(string $channel, Request $request): array
    {
        $driver = trim((string)$request->get('driver', ''));
        if ($driver === '') {
            return ['code' => 1, 'msg' => ''];
        }

        return $this->core()->templateOptions($channel, $driver);
    }


    /**
     * 获取渠道消息配置
     * @param string $channel 渠道键
     * @return array
     */
    public function messageConfig(string $channel): array
    {
        return $this->message()->config($channel);
    }


    /**
     * 保存渠道消息配置
     * @param string $channel 渠道键
     * @param Request $request 请求对象
     * @return array
     */
    public function saveMessageConfig(string $channel, Request $request): array
    {
        $items = $request->post('items');
        if (!is_array($items)) {
            return ['code' => 1, 'msg' => ''];
        }
        return $this->message()->save($channel, $items);
    }


    /**
     * 获取通知中心配置
     * @return array
     */
    public function notificationCenter(): array
    {
        return ['code' => 0, 'data' => $this->notification()->config()];
    }


    /**
     * 保存通知中心配置
     * @param Request $request 请求对象
     * @return array
     */
    public function saveNotificationCenter(Request $request): array
    {
        $data = $request->post('data');
        if (!is_array($data)) {
            return ['code' => 1, 'msg' => ''];
        }
        return $this->notification()->save($data);
    }

    /**
     * 获取渠道核心服务实例
     * @return CoreChannelService
     */
    private function core(): CoreChannelService
    {
        return $this->app->make(CoreChannelService::class);
    }

    /**
     * 获取渠道消息服务实例
     * @return CoreChannelMessageService
     */
    private function message(): CoreChannelMessageService
    {
        return $this->app->make(CoreChannelMessageService::class);
    }

    /**
     * 获取底部导航服务实例
     * @return CoreBottomNavService
     */
    private function bottomNav(): CoreBottomNavService
    {
        return $this->app->make(CoreBottomNavService::class);
    }

    /**
     * 获取通知中心服务实例
     * @return CoreNotificationCenterService
     */
    private function notification(): CoreNotificationCenterService
    {
        return $this->app->make(CoreNotificationCenterService::class);
    }
}