package com.oo.seex.oo_statistics_lib.bean;

import android.content.Context;
import android.text.TextUtils;

import com.oo.seex.oo_statistics_lib.utils.CommonUtil;
import com.oo.seex.oo_statistics_lib.utils.xutils_dbutils.db.annotation.Id;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by efort on 15/7/31.
 */
public class EventStaBean {
    @Id
    private int id;
    private String event_id;
    private String label;
    private String device_id;
    private String userId;
    //    private String clientdata_id;
    private String acc;
    private String remarks;
    private String time;
    private String activity;
    private String version;
    private String appkey;

    public EventStaBean() {

    }

    public EventStaBean(String event_id, String label, String acc, Context context, Map<String, String> remarks) {
        super();
        this.event_id = TextUtils.isEmpty(event_id) ? "" : event_id;
        this.label = TextUtils.isEmpty(label) ? "" : label;
        this.acc = TextUtils.isEmpty(acc) ? "" : acc;
//        this.remarks = TextUtils.isEmpty(remarks) ? "" : remarks;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            if (remarks != null && remarks.size() > 0) {
                Iterator iter = remarks.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String key = (String) entry.getKey();
                    String val = (String) entry.getValue();
                    if (!TextUtils.isEmpty(key)) {
                        stringBuilder.append(key);
                    } else {
                        continue;
                    }
                    stringBuilder.append("=");
                    if (!TextUtils.isEmpty(val)) {
                        stringBuilder.append(val);
                    } else {
                        stringBuilder.append("");
                    }
                    stringBuilder.append(",");
                }
                if (stringBuilder.toString().contains(","))
                    stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(","));
            }
        } catch (NullPointerException e) {
            CommonUtil.printLog("value中有空值", e.getMessage());
        }
        this.remarks = stringBuilder.toString();

        this.time = String.valueOf(CommonUtil.getTime());
        this.activity = CommonUtil.getActivityName(context);
        this.appkey = CommonUtil.getAppKey(context);
        this.version = CommonUtil.getVersion(context);

        this.userId = CommonUtil.getUserIdentifier(context);
        this.device_id = CommonUtil.getDeviceID(context);

    }

//    public EventStaBean(String event_id, String label, String acc, String time, String activity,
//                        String version, String appkey) {
//        super();
//        this.event_id = event_id;
//        this.label = label;
//        this.acc = acc;
//        this.time = time;
//        this.activity = activity;
//        this.version = version;
//        this.appkey = appkey;
//    }

    public boolean verification() {
        if (getAcc().contains("-") || getAcc() == null || getAcc().equals("")) {
            return false;
        } else {
            return true;
        }
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getActivity() {
        return activity;
    }

    public void setActivity(String activity) {
        this.activity = activity;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getAppkey() {
        return appkey;
    }

    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }

    public String getEvent_id() {
        return event_id;
    }

    public void setEvent_id(String event_id) {
        this.event_id = event_id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getAcc() {
        return acc;
    }

    public void setAcc(String acc) {
        this.acc = acc;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((acc == null) ? 0 : acc.hashCode());
        result = prime * result + ((activity == null) ? 0 : activity.hashCode());
        result = prime * result + ((appkey == null) ? 0 : appkey.hashCode());
        result = prime * result + ((event_id == null) ? 0 : event_id.hashCode());
        result = prime * result + ((label == null) ? 0 : label.hashCode());
        result = prime * result + ((time == null) ? 0 : time.hashCode());
        result = prime * result + ((version == null) ? 0 : version.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        EventStaBean other = (EventStaBean) obj;
        if (acc == null) {
            if (other.acc != null)
                return false;
        } else if (!acc.equals(other.acc))
            return false;
        if (activity == null) {
            if (other.activity != null)
                return false;
        } else if (!activity.equals(other.activity))
            return false;
        if (appkey == null) {
            if (other.appkey != null)
                return false;
        } else if (!appkey.equals(other.appkey))
            return false;
        if (event_id == null) {
            if (other.event_id != null)
                return false;
        } else if (!event_id.equals(other.event_id))
            return false;
        if (label == null) {
            if (other.label != null)
                return false;
        } else if (!label.equals(other.label))
            return false;
        if (time == null) {
            if (other.time != null)
                return false;
        } else if (!time.equals(other.time))
            return false;
        if (version == null) {
            if (other.version != null)
                return false;
        } else if (!version.equals(other.version))
            return false;
        return true;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRemarks() {
        return remarks;
    }

    public Map<String, String> getRemarkByMap() {
        HashMap<String, String> map = null;
        try {
            if (!TextUtils.isEmpty(remarks)) {
                map = new HashMap<String, String>();
                String[] remakeArrays = remarks.split(",");
                if (remakeArrays == null || remakeArrays.length == 0)
                    return null;
                for (int i = 0; i < remakeArrays.length; i++) {
                    String[] tempItem = remakeArrays[i].split("=");
                    if (tempItem != null && tempItem.length > 1 && tempItem[0] != null && tempItem[1] != null)
                        map.put(tempItem[0], tempItem[1]);
                }
            }
        } catch (Exception e) {
            CommonUtil.printLog("OOAgent", "getRemarkMap in getJsonData happen exception:" + e.getMessage());
        }
        return map;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getDevice_id() {
        return device_id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}
