package cm.sip.hns;

import android.app.Activity;
import android.app.Notification;
import android.os.Build;
import android.os.Vibrator;
import android.util.Log;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Timer;

import milsip.stack.sip.SIPCall;
import milsip.stack.sip.SIPStack;

public class SIPController {

    static String TAG = "SIPController";

    public static SIPSound sipSound = null;
    public static RTPManager audioRTPManager = null;
    public static SIPCall sipCall = null;
    public static SIPCall pendingCall = null;
    public static Notification notification = null;
    public static Timer BACKGROUNDTimer = null;
    public static Activity ownerContent = null;
    public static Activity mainContent = null;
    public static Vibrator vibe = null;
    public static boolean bMulticallSupport = true;
    public static Forward forward = new Forward();
    public static DatagramPacket dp = null;
    public static int latestMenu = 0;
    public static boolean bMulticallBusyResponse = true;

    public static void init() {
        Log.i(TAG, "======== SYSTEM INFO. =======");
        Log.i(TAG, "modelName is : " + Build.MODEL);
        Log.i(TAG, "device is : " + Build.DEVICE);
        Log.i(TAG, "ProductName is : " + Build.PRODUCT);
        Log.i(TAG, "ID is : " + Build.ID);
        Log.i(TAG, "USER is : " + Build.USER);
        Log.i(TAG, "TAGS is : " + Build.TAGS);
        Log.i(TAG, "VERSION is : " + Build.VERSION.SDK_INT);
        Log.i(TAG, "INCREMENTAL is : " + Build.VERSION.INCREMENTAL);
        Log.i(TAG, "RELEASE is : " + Build.VERSION.RELEASE);

        SIPStack.hwModelName = Build.MODEL;
        SIPStack.hwId = Build.ID;
        SIPStack.firmVersion = Build.VERSION.RELEASE;
    }

    public static void initSound() {   // SOUND DEVICE PREPARE
        try {
            sipSound = new SIPSound();
        } catch (Exception e) {
            System.err.println("SIPSound unavailable: " + e);
            SIPStack.exceptionCountAtCurrentCall++;
            return;
        }

        for (int i = 0; i < SIPStack.SIP_MEDIA_PORTS; i++) {
            SIPStack.SIP_MEDIA_PORTFLAG[i] = false;
        }

        return;
    }

    public static String getSHVE140SLocalIpAddress() {
        String validIp = null;
        boolean bWifi = false;
        boolean bEthernet = false;

        try {
            Enumeration en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = (NetworkInterface) en.nextElement();
                bWifi = false;
                bEthernet = false;
                if (intf != null && intf.getName().length() > 0 && intf.getName().startsWith("wlan")) {
                    bWifi = true;
                } else if (intf != null && intf.getName().length() > 0 && intf.getName().startsWith("eth")) {
                    bEthernet = true;
                }

                Enumeration enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                    String strIp = null;
                    if (inetAddress.toString() != null && inetAddress.toString().length() > 1) {
                        strIp = inetAddress.toString().substring(1);
                        StringTokenizer st = new StringTokenizer(strIp, ".", true);
                        int tokenCount = 0;
                        String token = null;

                        int ipdigit_0 = 0;
                        int ipdigit_1 = 0;
                        int ipdigit_2 = 0;
                        int ipdigit_3 = 0;
                        while (st.hasMoreTokens()) {
                            token = st.nextToken().trim();
                            if (token != null && token.length() > 0 && token.length() <= 3 && token.compareTo(".") != 0) {
                                if (tokenCount == 0) {
                                    ipdigit_0 = Integer.parseInt(token.trim());
                                } else if (tokenCount == 1) {
                                    ipdigit_1 = Integer.parseInt(token.trim());
                                } else if (tokenCount == 2) {
                                    ipdigit_2 = Integer.parseInt(token.trim());
                                } else if (tokenCount == 3) {
                                    ipdigit_3 = Integer.parseInt(token.trim());
                                }
                                tokenCount++;
                            }
                        }

                        if (tokenCount == 4) {
                            if (ipdigit_0 > 0 && ipdigit_0 <= 255 && ipdigit_1 >= 0 && ipdigit_1 <= 255 && ipdigit_2 >= 0 && ipdigit_2 <= 255 && ipdigit_3 >= 0 && ipdigit_3 <= 255 &&
                                    !(ipdigit_0 == 127 && ipdigit_1 == 0 && ipdigit_2 == 0 && ipdigit_3 == 1)) {   // wifi is wlan0

                                boolean bLast = true;
                                // that is sk and etc all case
                                if (!bLast || bEthernet || bWifi) {
                                    validIp = ipdigit_0 + "." + ipdigit_1 + "." + ipdigit_2 + "." + ipdigit_3;
                                    Log.d(TAG, "[getSHVE140SLocalIpAddress] validIp 1 [" + validIp + "]");
                                    return validIp;
                                } else if (bLast) {
                                    validIp = ipdigit_0 + "." + ipdigit_1 + "." + ipdigit_2 + "." + ipdigit_3;
                                }
                            }
                        }
                    }
                }
            }
        } catch (SocketException exception) {
            Log.e(TAG, "[Excetpion] getSHVE140SLocalIpAddress()");
            SIPStack.exceptionCountAtCurrentCall++;
        }
        Log.d(TAG, "[getSHVE140SLocalIpAddress] validIp 2 [" + validIp + "]");
        return validIp;
    }

}
