package org.mahjong4j.hands;

import org.mahjong4j.IllegalMentsuSizeException;
import org.mahjong4j.Mahjong4jException;

import java.util.ArrayList;
import java.util.List;

/**
 * 上がれる面子を整理したクラスです
 *
 * @author yu1ro
 */
public class MentsuComp {

    private List<Toitsu> toitsuList = new ArrayList<>(7);
    private List<Shuntsu> shuntsuList = new ArrayList<>(4);
    private List<Kotsu> kotsuList = new ArrayList<>(4);
    private List<Kantsu> kantsuList = new ArrayList<>(4);

    /**
     * @param mentsuList 上がりとなった面子のリスト
     * @throws IllegalMentsuSizeException 和了れる形になっていなければthrow
     */
    public MentsuComp(List<MahjongMentsu> mentsuList) throws Mahjong4jException {
        for (MahjongMentsu mentsu : mentsuList) {
            setMentsu(mentsu);
        }

        //整合性を確認する
        int checkSum = shuntsuList.size() + kotsuList.size() + kantsuList.size();
        boolean isNormal = checkSum == 4 && toitsuList.size() == 1;
        boolean isChitoitsu = toitsuList.size() == 7 && checkSum == 0;
        if (!isNormal && !isChitoitsu) {
            throw new IllegalMentsuSizeException(mentsuList);
        }
    }

    /**
     * どの面子が入っても対応可能なセッター
     *
     * @param mentsu 入力したい面子
     */
    private void setMentsu(MahjongMentsu mentsu) {
        if (mentsu instanceof Toitsu) {
            toitsuList.add((Toitsu) mentsu);
        } else if (mentsu instanceof Shuntsu) {
            shuntsuList.add((Shuntsu) mentsu);
        } else if (mentsu instanceof Kotsu) {
            kotsuList.add((Kotsu) mentsu);
        } else if (mentsu instanceof Kantsu) {
            kantsuList.add((Kantsu) mentsu);
        }
    }

    public List<Toitsu> getToitsuList() {
        return toitsuList;
    }

    public int getToitsuNum() {
        return toitsuList.size();
    }

    public List<Shuntsu> getShuntsuList() {
        return shuntsuList;
    }

    public int getShuntsuNum() {
        return shuntsuList.size();
    }

    public List<Kotsu> getKotsuList() {
        return kotsuList;
    }

    public int getKotsuNum() {
        return kotsuList.size();
    }

    public List<Kantsu> getKantsuList() {
        return kantsuList;
    }

    public int getKantsuNum() {
        return kantsuList.size();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MentsuComp)) return false;

        MentsuComp that = (MentsuComp) o;

        if (toitsuList != null ? !toitsuList.equals(that.toitsuList) : that.toitsuList != null) return false;
        if (shuntsuList != null ? !shuntsuList.equals(that.shuntsuList) : that.shuntsuList != null) return false;
        if (kotsuList != null ? !kotsuList.equals(that.kotsuList) : that.kotsuList != null) return false;
        return !(kantsuList != null ? !kantsuList.equals(that.kantsuList) : that.kantsuList != null);

    }

    @Override
    public int hashCode() {
        int result = toitsuList != null ? toitsuList.hashCode() : 0;
        result = 31 * result + (shuntsuList != null ? shuntsuList.hashCode() : 0);
        result = 31 * result + (kotsuList != null ? kotsuList.hashCode() : 0);
        result = 31 * result + (kantsuList != null ? kantsuList.hashCode() : 0);
        return result;
    }
}

@@ -1624,32 +1624,244 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * %E4%B8%83%E5%AF%BE%E5%AD%90%E3%81%AE%E5%A0%B4%E5%90%88%E3%81%AFnull%E3%82%92%E8%BF%94%E3%81%97%E3%81%BE%E3%81%99%0A     *%0A     * @return %E9%9B%80%E9%A0%AD%E3%82%92%E8%BF%94%E3%81%97%E3%81%BE%E3%81%99%0A     */%0A    public Toitsu getJanto() %7B%0A        if (getToitsuNum() == 1) %7B%0A            return toitsuList.get(0);%0A        %7D%0A        return null;%0A    %7D%0A%0A
     public List%3C
@@ -3058,17 +3058,16 @@
  null);%0A
-%0A
     %7D%0A%0A 

add getJanto