package com.imofan.android.basic;

import android.util.Log;

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

/**
 * 访问路径封装类
 */
class MFStatAccessPath {
    private final static String LOG_TAG = Mofang.LOG_TAG + "_MFStatAccessPath";

    //增加访问界面信息
    static void addAccessNode(int id, String node, long duration) {
        String accessPath = MFStatInfo.getString(MFStatInfo.KEY_USER_ACCESS_PATH, null);
        if(accessPath == null || accessPath.trim().length() == 0) {
            accessPath = "";
        } else {
            accessPath += ";";
        }
        if(node != null && node.trim().length() > 0) {
            node = node.trim().replaceAll(",", "\\co\\").replaceAll(";", "\\se\\")
                    .replaceAll(Node.EXIT_NODE, "\\" + Node.EXIT_NODE + "\\");
        } else {
            node = "Exit";
        }
        accessPath += id + "," + node + "," + duration;
        MFStatInfo.putString(MFStatInfo.KEY_USER_ACCESS_PATH, accessPath);
    }

    //增加访问界面信息
    static void addAccessNode(int id, String node) {
        addAccessNode(id, node, 0);
    }

    //更新访问时长信息
    static void updateAccessDuration(int id, long duration) {
        String accessPath = MFStatInfo.getString(MFStatInfo.KEY_USER_ACCESS_PATH, null);
        if(accessPath == null || accessPath.trim().length() == 0) {
            return;
        }
        if(Mofang.DEBUG) {
            Log.d(LOG_TAG, "[updateAccessDuration] id = " + id  + ", accessPath = " + accessPath);
            //[updateAccessDuration] id = 719155321, accessPath =
        }
        String str2 = ";" + id + ",";
        String[] str1And3456 = accessPath.split(str2);
        if(str1And3456.length == 1) {
            str2 = id + ",";
            str1And3456 = new String[2];
            str1And3456[0] = "";
            str1And3456[1] = accessPath.substring(accessPath.indexOf(",") + 1);
        }
        String str1 = str1And3456[0];
        String str3 = str1And3456[1].substring(0, str1And3456[1].indexOf(","));
        String str4 = ",";
        String str56 = str1And3456[1].substring(str1And3456[1].indexOf(",") + 1);
        String str5 = Long.toString(duration);
        String str6 = "";
        if(str56.indexOf(";") > 0) {
            str6 = str56.substring(str56.indexOf(";"));
        }
        accessPath = str1 + str2 + str3 + str4 + str5 + str6;
        MFStatInfo.putString(MFStatInfo.KEY_USER_ACCESS_PATH, accessPath);
    }

    //提取所有访问路径
    static List<Node> getAccessNodes() {
        List<Node> accessNodes = null;
        String accessPath = MFStatInfo.getString(MFStatInfo.KEY_USER_ACCESS_PATH, null);
        if(accessPath != null && accessPath.trim().length() > 0) {
            accessNodes = new ArrayList<Node>();
            String[] paths = accessPath.split(";");
            for(String path : paths) {
                if(path.trim().length() > 0) {
                    Node accessNode = new Node();
                    String[] info = path.trim().split(",");
                    if(!info[1].equals(Node.EXIT_NODE)) {
                        info[1] = info[1].replaceAll("\\\\co\\\\", ",").replaceAll("\\\\se\\\\", ";")
                                .replaceAll("\\\\" + Node.EXIT_NODE + "\\\\", Node.EXIT_NODE);
                        accessNode.setActivity(info[1]);
                    }
                    accessNode.setDuration(Long.parseLong(info[2]));
                    accessNodes.add(accessNode);
                }
            }
        }
        return accessNodes;
    }

    //记录访问节点信息的封装类
    static class Node {
        private final static String EXIT_NODE = "Exit";

        private String activity;    //节点名称
        private int count;          //访问次数
        private int exit;           //跳出次数
        private long duration;     //停留时长

        public String getActivity() {
            return activity;
        }

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

        public void incCount() {
            count++;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public void incExit() {
            exit++;
        }

        public int getExit() {
            return exit;
        }

        public void setExit(int exit) {
            this.exit = exit;
        }

        public long getDuration() {
            return duration;
        }

        public void setDuration(long duration) {
            this.duration = duration;
        }
    }
}
