package com.miya.fastcashier.util.manage;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.fastcashier.lib_common.function.account.AccountService;
import com.google.gson.reflect.TypeToken;
import com.miya.fastcashier.bean.ChannelInfoBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类说明：渠道管理类（用于处理品牌全局设置，以及换肤所需渠道配置）
 * 详细描述：目前通过本地资源文件加载  todo 后期能改成接口返回渠道最好
 * 创建人：zpxiang
 * 创建时间：
 * 修改人：
 * 修改时间：
 */
public class ChannelManageKit {

    private static Map<String, ChannelInfoBean> channelMap;
    private static final String CHANNEL_OFFICIAL_ACCOUNT = "-1";
    private static final String CHANNEL_OFFICIAL_CHANNELNAME = "通用";

    /**
     * 初始化本地渠道数据
     */
    public static void initChannelRes(Context context) {
        clearChannelResData();
        if (null == context) {
            initDefaultChannelRes();
            return;
        }

        String assetsJson = getAssetsJson(context, "channel.json");
        if (TextUtils.isEmpty(assetsJson)) {
            initDefaultChannelRes();
            return;
        }
        ArrayList<ChannelInfoBean> channelInfoList = GsonUtils.fromJson(assetsJson, new TypeToken<List<ChannelInfoBean>>() {}.getType());
        channelMap = new HashMap<>();
        if (!channelInfoList.isEmpty()) {
            for (int i = 0; i < channelInfoList.size(); i++) {
                ChannelInfoBean channelInfoBean = channelInfoList.get(i);
                List<String> channelAccountArray = channelInfoBean.getChannelAccountArray();
                if (channelAccountArray.isEmpty()){
                    initDefaultChannelRes();
                    return;
                }

                for (int j = 0; j < channelAccountArray.size(); i++) {
                    String account = channelAccountArray.get(j);
                    channelMap.put(account,channelInfoBean);
                }
            }
        }
    }

    private static void initDefaultChannelRes() {
        channelMap = new HashMap<>();
        ChannelInfoBean commonChannel = new ChannelInfoBean();
        commonChannel.setChannelId("-1");
        commonChannel.setChannelName(CHANNEL_OFFICIAL_CHANNELNAME);
        commonChannel.setChannelNameEn(CHANNEL_OFFICIAL_CHANNELNAME);
        channelMap.put(CHANNEL_OFFICIAL_ACCOUNT,commonChannel);
    }

    /**
     * 获取渠道名称
     */
    public static String getAppChannelName(Context context) {
        if (channelMap == null || channelMap.isEmpty()) {
            initChannelRes(context);
        }

        boolean containsKey = channelMap.containsKey(AccountService.INSTANCE.getUserName());
        if (!containsKey) {
            return CHANNEL_OFFICIAL_CHANNELNAME;
        }
        return channelMap.get(AccountService.INSTANCE.getUserName()).getChannelName();
    }

    /**
     * 获取App渠道编号
     */
    public static String getAppChannelId(Context context) {
        if (context == null) {
            return CHANNEL_OFFICIAL_ACCOUNT;
        }

        if (channelMap == null || channelMap.isEmpty()) {
            initChannelRes(context);
        }

        if (!channelMap.containsKey(AccountService.INSTANCE.getUserName())) {
            return CHANNEL_OFFICIAL_ACCOUNT;
        }
        return channelMap.get(AccountService.INSTANCE.getUserName()).getChannelId();
    }

    public static void clearChannelResData() {
        if (null == channelMap) {
            return;
        }

        channelMap.clear();
        channelMap = null;
    }

    /**
     * 读取json文件
     */
    private static String getAssetsJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        AssetManager assetManager = context.getAssets();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName), "UTF-8"));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }
}
