package com.genlot.ninglottery.game;

import android.util.ArrayMap;

import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.model.GameRuntimeAttr;
import com.genlot.nativesdk.model.GameTicket;
import com.genlot.ninglottery.model.GameIDSet;
import com.genlot.ninglottery.model.GameName;
import com.genlot.ninglottery.model.PlayTypeID;
import com.genlot.ninglottery.model.PlayTypeName;

import java.lang.reflect.Field;

/**
 * @author ZhuYongKang
 * @description GameManager
 * @date 2019/8/19
 */
public class GameManager {
    private static ArrayMap<Integer, GameRuntimeAttr> mGameRuntime = new ArrayMap<>();
    private static GameTicket mLastTicket;
    private static double[] mAccount;
    /**
     * 登录用户名
     */
    private static String mUser;
    /**
     * 登录用户密码
     */
    private static String mPassword;
    /**
     * 站地址
     */
    private static String stationAddress = "";
    /**
     * 是否培训模式
     */
    public static boolean isTrainMode;

    public static void initManager() {
        mAccount = new double[2];
    }

    public static void setAccount(double[] value) {
        mAccount[0] = value[0];
        mAccount[1] = value[1];
    }

    public static double[] getAccount() {
        return mAccount;
    }

    public static String getStationAddress() {
        return stationAddress;
    }

    public static void setStationAddress(String stationAddress) {
        GameManager.stationAddress = stationAddress;
    }

    public static String getUser() {
        return mUser;
    }

    public static void setUser(String user) {
        mUser = user;
    }

    public static String getPassword() {
        return mPassword;
    }

    public static void setPassword(String password) {
        mPassword = password;
    }

    public static void initLastTicket() {
        mLastTicket = new GameTicket();
        SystemNI.readLastTicket(mLastTicket);
    }

    public static GameTicket getLastTicket() {
        SystemNI.readLastTicket(mLastTicket);
        return mLastTicket;
    }

    public static void appendGameRuntime(GameRuntimeAttr attr) {
        if (attr != null) {
            mGameRuntime.put(attr.mGameID, attr);
        }
    }

    public static void removeGameRuntime(int gameId) {
        mGameRuntime.remove(gameId);
    }

    public static void clearGameRuntime() {
        mGameRuntime.clear();
    }

    public static int getGameRuntimeSize() {
        return mGameRuntime.size();
    }

    public static ArrayMap<Integer, GameRuntimeAttr> getGameRuntime() {
        return mGameRuntime;
    }

    public static GameRuntimeAttr getGameRuntime(int gameID) {
        if (mGameRuntime.containsKey(gameID)) {
            return mGameRuntime.get(gameID);
        }
        return null;
    }

    /**
     * 获得游戏名
     * @param gameId   gameId
     * @return  游戏名
     */
    public static String getGameName(int gameId) {
        Field[] fields = GameIDSet.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(GameName.class)) {
                try {
                    if ((int)field.get(null) == gameId) {
                        GameName name = field.getAnnotation(GameName.class);
                        return name.name();
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    /**
     * 获得玩法名
     * @param playTypeId   id
     * @return  游戏名
     */
    public static String getPlayTypeName(int playTypeId) {
        Field[] fields = PlayTypeID.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(PlayTypeName.class)) {
                try {
                    if ((int)field.get(null) == playTypeId) {
                        PlayTypeName name = field.getAnnotation(PlayTypeName.class);
                        return name.name();
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    /**
     * 获得玩法ID
     * @param name 玩法名
     * @return ID
     */
    public static int getPlayTypeID(String name) {
        Field[] fields = PlayTypeID.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(PlayTypeName.class)) {
                try {
                    PlayTypeName playTypeName = field.getAnnotation(PlayTypeName.class);
                    if (playTypeName.name().equals(name)) {
                        return (int) field.get(null);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

}
