package com.sheep.gamegroup.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.text.TextUtils;

import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 16/12/28.
 */
public class ChannelContent {

    private static final String META_INF_FILE_START = "META-INF/pl_channel_";
    private static final String CHANNEL_FILE_MARK = "pl_channel_";
    private String channel_name="";
    private String gameId;
    private static ChannelContent instance;
    private Properties properties;
    private boolean hasChannelFile = false;

    public static ChannelContent getInstance() {
        if (null == instance) {
            instance = new ChannelContent();
        }
        return instance;
    }

    private ChannelContent() {
    }

    public boolean initChannelContent(Context context) {
        hasChannelFile = false;
        ApplicationInfo appInfo = context.getApplicationInfo();
        String sourceDir = appInfo.sourceDir;
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();
                if (entryName.contains("../")) {
                    break;
                }
                if (entryName.startsWith(META_INF_FILE_START)) {
                    if (entryName.contains(".")) {
                        break;
                    }
                    String[] split = entryName.split(CHANNEL_FILE_MARK);
                    channel_name = split[split.length - 1];
                    hasChannelFile = true;
                    long size = entry.getSize();
                    if (size > 0) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(zipfile.getInputStream(entry)));
                        properties = new Properties();
                        properties.load(br);
                        br.close();
                    }
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if(!hasChannelFile){//zip方式获取渠道
            try {
                PackageInfo packageInfo = ApkUtils.getPackageInfo(SheepApp.getInstance().getPackageName());
                if(packageInfo != null && !TextUtils.isEmpty(packageInfo.applicationInfo.sourceDir)) {
                    String comment = ZipChannelUtil.readQUA(new File(packageInfo.applicationInfo.sourceDir));
                    if(!TextUtils.isEmpty(comment)){
                        hasChannelFile = true;
                        if(comment.contains(";")){
                            String[] items = comment.split(";");
                            channel_name = items[0];
                            gameId = items[1];
                        } else {
                            channel_name = comment;
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return hasChannelFile;
    }
    public String getChannel_name() {
        return channel_name;
    }

    public Map<String, String> getChanelInfo(String... key) {
        if (!hasChannelFile || properties == null || properties.isEmpty()) {
            new ExceptionInInitializerError("has your initChannelContent() ?").printStackTrace();
            return null;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        for (String s : key) {
            hashMap.put(s, properties.getProperty(s, ""));
        }
        return hashMap;
    }

    public String getGameId() {
        return gameId;
    }
}