package mahjong.aux;

/**
 * Created by joe on 12/1/14.
 */
public class Card {
    int id = 0;
    static final String[] typeName = {"Wan","Tong","Tiao","Feng","Yuan"};
    static final String[] typeSuffix = {"Wan","Tong","Tiao","Feng",""};
    static final String[] shuName = {"Guard","Guard","Yi","Er","San","Si","Wu","Liu","Qi","Ba","Jiu","Guard","Guard"};
    static final String[] fengName = {"Guard","Guard","Dong","Guard","Guard","Nan","Guard","Guard","Xi","Guard","Guard","Bei","Guard","Guard"};
    static final String[] yuanName = {"Guard","Guard","Fa","Guard","Guard","Zhong","Guard","Guard","Bai","Guard","Guard"};
    static final String exName = "Hong";
    static final String errName = "ERROR";

    static public int parseSuffixNameId(String str)
    {
        int id;
        for (int i=0;i<typeSuffix.length;++i)
            if (str.compareToIgnoreCase(typeSuffix[i])==0)
                return i;
        return 0;
    }
    static public int parseOrdNameId(String str)
    {
        int id;
        for (int i=0;i<shuName.length;++i)
            if (str.compareToIgnoreCase(shuName[i])==0)
                return i;
        for (int i=0;i<fengName.length;++i)
            if (str.compareToIgnoreCase(fengName[i])==0)
                return i;
        for (int i=0;i<yuanName.length;++i)
            if (str.compareToIgnoreCase(yuanName[i])==0)
                return i;
        return 0;
    }
    static public int parseExId(String str)
    {
        if (str.compareToIgnoreCase(exName)==0)
            return 1;
        return 0;
    }

    // Various Constructor
    public Card(){}
    public Card(int id)
    {
        this.id=id;
    }
    public Card(String name)
    {
        int type = 4;
        int ord = 0;
        int ex = 0;
        if (name.charAt(0)==':')
        {
            ord = name.charAt(1)-47;
            switch (name.charAt(2))
            {
                case 'M':
                    type=0;break;
                case 'P':
                    type=1;break;
                case 'S':
                    type=2;break;
                case 'F':
                    type=3;break;
                case 'Z':
                    type=4;break;
            }
            if (name.length()>3)
                ex=1;
            id = (((type<<4)+ord)<<1)+ex;
            return;
        }
        String[] res = name.split("\\s");

        ex=parseExId(res[0]);

        ord = parseOrdNameId(res[ex]);
        if (res.length > ex+1)
            type = parseSuffixNameId(res[ex+1]);
        id = (((type<<4)+ord)<<1)+ex;
    }

    // Some Getters
    public int getId() {
        return id;
    }
    public int getStdId() {return id&0xFFFE;}
    public int getTypeId()
    {
        return id>>5;
    }
    public int getOrdId()
    {
        return (id>>1)&0xF;
    }
    public int getExId()
    {
        return id&1;
    }
    public String getTypeName() {return typeName[getTypeId()];}
    public String getTypeSuffix() {return typeSuffix[getTypeId()];}
    public String getOrdName()
    {
        int type = getTypeId();
        int ord = getOrdId();
        if (isShu())
            return shuName[ord];
        if (isFeng())
            return fengName[ord];
        if (isYuan())
            return yuanName[ord];
        return errName;
    }
    public String getBaoName()
    {
        if ( getExId() > 0)
            return exName;
        return "";
    }
    public String getNormalName()
    {
        return getBaoName().concat(" ")
                .concat(getOrdName()).concat(" ")
                .concat(getTypeSuffix())
                .trim();
    }

    // Some Testers
    public boolean isShu()      { return getTypeId()<3; }
    public boolean isFeng()     { return getTypeId() == 3; }
    public boolean isYuan()     { return getTypeId() == 4; }
    public boolean isLaoTou()   { return isShu()&& (getOrdId()&7) == 0; }
    public boolean isZi()       { return isFeng() || isYuan(); }
    public boolean is19()       { return isLaoTou() || isZi();}
    public boolean isZhongZhang(){ return !is19(); }
}