package com.easygbs.easygbd.util;
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.easygbs.easygbd.application.App;
import com.easygbs.easygbd.common.Constant;
import com.easygbs.easygbd.dao.DCon;
import com.easygbs.easygbd.dao.DOpe;
import com.easygbs.easygbd.dao.bean.Chan;

import org.easydarwin.util.SIP;
import org.easydarwin.util.SPUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SipUtil {
	private static String TAG = SipUtil.class.getSimpleName();
	private Context mContext;
	private static SipUtil Instance = null;

	public static SipUtil getInstance(Context context){
		if (Instance == null) {
			Instance = new SipUtil(context);
		}

		return Instance;
	}

	private SipUtil(Context mContext){
		this.mContext=mContext;
	}

	public SIP getSIP(){
		SIP mSIP = new SIP();
		mSIP.setVer(SPUtil.getVer(mContext));
		mSIP.setServerIp(SPUtil.getServerip(mContext));
		mSIP.setServerPort(SPUtil.getServerport(mContext));
		mSIP.setLocalSipPort(SPUtil.getLocalsipport(mContext));
		mSIP.setServerId(SPUtil.getServerid(mContext));
		mSIP.setServerDomain(SPUtil.getServerdomain(mContext));
		mSIP.setDeviceId(SPUtil.getDeviceid(mContext));
		mSIP.setDeviceName(SPUtil.getDevicename(mContext));
		mSIP.setPassword(SPUtil.getPassword(mContext));
		mSIP.setProtocol(SPUtil.getProtocol(mContext));
		mSIP.setRegExpires(SPUtil.getRegexpires(mContext));
		mSIP.setHeartbeatInterval(SPUtil.getHeartbeatinterval(mContext));
		mSIP.setHeartbeatCount(SPUtil.getHeartbeatcount(mContext));

		List<Chan> ChanList = new ArrayList<Chan>();
		boolean per = PeUtil.ishasPer(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
		if(per){
				DOpe mDOpe = DOpe.Instance(mContext,mContext.getExternalFilesDir(null).getAbsolutePath() + File.separator + Constant.DIR);
				List<Chan> ChanListLocal = (List<Chan>)mDOpe.OperaDatabase(DCon.Chanqueryeffect,"");
				if (ChanListLocal.size() >= 1) {
					ChanList.addAll(ChanListLocal);
				}
		}

		List<SIP.GB28181_CHANNEL_INFO_T> GB28181_CHANNEL_INFO_TList = new ArrayList<>();

		for(int i=0;i<ChanList.size();i++) {
			Chan mChan=ChanList.get(i);
			SIP.GB28181_CHANNEL_INFO_T mGB28181_CHANNEL_INFO_T = new SIP.GB28181_CHANNEL_INFO_T();
			mGB28181_CHANNEL_INFO_T.setName(mChan.getNa());
			mGB28181_CHANNEL_INFO_T.setManufacturer("easygbd");
			mGB28181_CHANNEL_INFO_T.setModel(SPUtil.getDeviceid(mContext));
			mGB28181_CHANNEL_INFO_T.setParentId(SPUtil.getDeviceid(mContext));
			mGB28181_CHANNEL_INFO_T.setIndexCode(mChan.getCid());
			mGB28181_CHANNEL_INFO_T.setOwner("easygbd");
			mGB28181_CHANNEL_INFO_T.setCivilCode("");
			mGB28181_CHANNEL_INFO_T.setAddress("Address");
			mGB28181_CHANNEL_INFO_T.setLongitude(Double.parseDouble(SPUtil.getLongitude(mContext)));
			mGB28181_CHANNEL_INFO_T.setLatitude(Double.parseDouble(SPUtil.getLatitude(mContext)));

			GB28181_CHANNEL_INFO_TList.add(mGB28181_CHANNEL_INFO_T);
		}

		mSIP.setList(GB28181_CHANNEL_INFO_TList);

		return mSIP;
	}

} 