<?php

namespace Biz\SetAdapter\Result;

class AndroidAdapter extends BaseAdapter {

    /**
     * 安卓接口适配
     * @param string $path
     * @param array $params
     * @return array
     */
    public function update(string $path, array $params) {
        $params = parent::update($path, $params);
        $conf = lang('AndroidConf', 'api')->get($path);
        //按配置文件处理
        if (isset($conf)) {
            $oldStatus = $params['status'];
            $newStatus = $conf['status'][$oldStatus] ?? $oldStatus;
            $params['status'] = $newStatus;
            if (isset($conf['toArr']) && $conf['toArr'] === 1) {
                $params['data'] = array_values($params['data']);
            }
            if (isset($conf['arrFields'])) {
                foreach ($conf['arrFields'] as $k => $v) {
                    if (is_array($params['data'][$v])) continue;//如果已是数组则继续
                    if (empty(trim($params['data'][$v]))) $params['data'][$v] = [];
                }
            }
            if (isset($conf['conv'])) {
                $data = $params['data']['data'];
                foreach ($conf['conv'] as $k => $v) {
                    $targetField = $v['destField'];
                    $targetKey = $v['key'];
                    foreach ($data as $kk => $vv) {
                        if (isset($vv['settings'][$targetField])) {
                            if (is_string($vv['settings'][$targetField])) {
                                $vv['settings'][$targetField] = [$targetKey => floatval($vv['settings'][$targetField])];
                            }
                            if (isset($vv['settings'][$targetField]['min'])) {
                                $vv['settings'][$targetField]['min'] = floatval($vv['settings'][$targetField]['min']);
                            }
                            if (isset($vv['settings'][$targetField]['max'])) {
                                $vv['settings'][$targetField]['max'] = floatval($vv['settings'][$targetField]['max']);
                            }
                            $data[$kk] = $vv;
                        }
                    }
                }
                $params['data']['data'] = $data;
            }
        }
        self::fixSth($path, $params);
        return $params;
    }

    protected function fixSth(string $path, array &$params) {
        //register the fixFunctions
        self::fixExample($path, $params);
    }

    protected function fixExample(string $path, array &$params) {
        //do sth
    }
}
