package com.inspect.metadata.eqpbook.domain;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class EqpCollection<T> {
    String codeName;
    String idName;
    List<String> codes = new ArrayList<>();
    Map<String, Long> codesAndIds = new HashMap<>();
    List<T> insertList = new ArrayList<>();
    List<T> updateList = new ArrayList<>();

    public void loadEntity(T t) {
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(t));
        String code = json.getString(this.codeName);
        if (this.codes.contains(code)) {
            try {
                Method method = t.getClass().getMethod("set" + this.idName.substring(0, 1).toUpperCase() + this.idName.substring(1), new Class[]{Long.class});
                method.invoke(t, this.codesAndIds.get(code));
            } catch (Exception e) {
                e.printStackTrace();
            }

            this.updateList.add(t);
        } else {
            this.insertList.add(t);
        }

    }

    public static <T> EqpCollection create(List<T> list, String codeName, String idName) {
        EqpCollection eqpCollection = new EqpCollection<>();
        eqpCollection.codeName = codeName;
        eqpCollection.idName = idName;

        for (T t : list) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(t));
            eqpCollection.codes.add(jsonObject.getString(codeName));
            eqpCollection.codesAndIds.put(jsonObject.getString(codeName), jsonObject.getLong(idName));
        }

        return eqpCollection;
    }

    public String getCodeName() {
        return this.codeName;
    }

    public String getIdName() {
        return this.idName;
    }

    public List<String> getCodes() {
        return this.codes;
    }

    public Map<String, Long> getCodesAndIds() {
        return this.codesAndIds;
    }

    public List<T> getInsertList() {
        return this.insertList;
    }

    public List<T> getUpdateList() {
        return this.updateList;
    }

    public void setCodeName(String codeName) {
        this.codeName = codeName;
    }

    public void setIdName(String idName) {
        this.idName = idName;
    }

    public void setCodes(List<String> codes) {
        this.codes = codes;
    }

    public void setCodesAndIds(Map<String, Long> codesAndIds) {
        this.codesAndIds = codesAndIds;
    }

    public void setInsertList(List<T> insertList) {
        this.insertList = insertList;
    }

    public void setUpdateList(List<T> updateList) {
        this.updateList = updateList;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof EqpCollection)) {
            return false;
        } else {
            EqpCollection<?> other = (EqpCollection) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$codeName = this.getCodeName();
                Object other$codeName = other.getCodeName();
                if (this$codeName == null) {
                    if (other$codeName != null) {
                        return false;
                    }
                } else if (!this$codeName.equals(other$codeName)) {
                    return false;
                }

                Object this$idName = this.getIdName();
                Object other$idName = other.getIdName();
                if (this$idName == null) {
                    if (other$idName != null) {
                        return false;
                    }
                } else if (!this$idName.equals(other$idName)) {
                    return false;
                }

                Object this$codes = this.getCodes();
                Object other$codes = other.getCodes();
                if (this$codes == null) {
                    if (other$codes != null) {
                        return false;
                    }
                } else if (!this$codes.equals(other$codes)) {
                    return false;
                }

                label62:
                {
                    Object this$codesAndIds = this.getCodesAndIds();
                    Object other$codesAndIds = other.getCodesAndIds();
                    if (this$codesAndIds == null) {
                        if (other$codesAndIds == null) {
                            break label62;
                        }
                    } else if (this$codesAndIds.equals(other$codesAndIds)) {
                        break label62;
                    }

                    return false;
                }

                label55:
                {
                    Object this$insertList = this.getInsertList();
                    Object other$insertList = other.getInsertList();
                    if (this$insertList == null) {
                        if (other$insertList == null) {
                            break label55;
                        }
                    } else if (this$insertList.equals(other$insertList)) {
                        break label55;
                    }

                    return false;
                }

                Object this$updateList = this.getUpdateList();
                Object other$updateList = other.getUpdateList();
                if (this$updateList == null) {
                    if (other$updateList != null) {
                        return false;
                    }
                } else if (!this$updateList.equals(other$updateList)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof EqpCollection;
    }

    public int hashCode() {
        int PRIME = 1;
        int result = 1;
        Object $codeName = this.getCodeName();
        result = result * 59 + ($codeName == null ? 43 : $codeName.hashCode());
        Object $idName = this.getIdName();
        result = result * 59 + ($idName == null ? 43 : $idName.hashCode());
        Object $codes = this.getCodes();
        result = result * 59 + ($codes == null ? 43 : $codes.hashCode());
        Object $codesAndIds = this.getCodesAndIds();
        result = result * 59 + ($codesAndIds == null ? 43 : $codesAndIds.hashCode());
        Object $insertList = this.getInsertList();
        result = result * 59 + ($insertList == null ? 43 : $insertList.hashCode());
        Object $updateList = this.getUpdateList();
        result = result * 59 + ($updateList == null ? 43 : $updateList.hashCode());
        return result;
    }

    public String toString() {
        return "EqpCollection(codeName=" + this.getCodeName() + ", idName=" + this.getIdName() + ", codes=" + this.getCodes() + ", codesAndIds=" + this.getCodesAndIds() + ", insertList=" + this.getInsertList() + ", updateList=" + this.getUpdateList() + ")";
    }
}
