package com.xixun.communicate;

import com.xixun.display.MyLog;

import java.net.Socket;
import java.util.LinkedList;

/**
 * Created by Administrator on 17/3/31.
 */

public class ClientManage {
	ClientsManage clientsManage=null;
	private String clientIpAddress=null;
    public SendFile sendFile=null;
    public TcpCommunication tcpCommunication=null;

    private LinkedList sendFileAndCommandList = null;

    ClientManage(ClientsManage clientsManage,String clientIpAddress){

        sendFileAndCommandList = new LinkedList();

        this.clientsManage=clientsManage;
        // 先建立套接字接受服务程序；

        // 获取到IP地址；
        this.clientIpAddress=clientIpAddress;
        //MyLog.i("Clinet Display IP:" +clientIpAddress + " Connectted!");
        /*
        new Thread() {
            public void run() {
                BackgroundSendFileAndCommand();
            }
        }.start();
        */

        new Thread() {
            public void run() {
                Process();
            }
        }.start();

    }


    public void TransData_Byte(byte ucCommand,byte ucIdence, byte ucPipeId, byte[] DataBuffer, int iLength)
    {
        if (NeedExit()) return;

        if (tcpCommunication != null)
            tcpCommunication.TransData_Byte(ucCommand, ucIdence, ucPipeId,DataBuffer, iLength);
    }

    public Boolean IdenceJundge(String strIdence)
    {
        if (strIdence == null) return true;
        if (strIdence.equals(GetIpAddress())) return true;

        return false;
    }


    public String GetIpAddress()
    {
        return clientIpAddress;
    }

    public void SettcpCommunicationSocket(Socket socket)
    {   if (tcpCommunication!=null) tcpCommunication.Exit();
        tcpCommunication = new TcpCommunication(this,socket);
    }

    public void SetSendFileSocket(Socket socket)
    {   if (sendFile!=null) sendFile.Exit();
        sendFile = new SendFile(this,socket);
    }

	public boolean Client_IdenceJundge(String strClientIdence)
	{   if (strClientIdence==null) return true;
        if (strClientIdence.equals(Get_ClientIpAddress())) return true;
		if (strClientIdence.equals(Get_DisplayID())) return true;
		return false;
	}

    public String Get_DisplayID()
    {   if (tcpCommunication!=null)
	        return tcpCommunication.GetDisplayID();
	    return null;
    }

	private void BackgroundSendFileAndCommand() {
        while (NeedExit()==false) {
            if (sendFileAndCommandList.size() != 0) {
                FileBackground background = (FileBackground) sendFileAndCommandList.getFirst();
                try {
	                if (background!=null)
                    {   if (sendFile!=null) sendFile.SendFilePro(background.pathFile, background.strObjectName);
	                    //if (tcpCommunication != null) tcpCommunication.TransCommand(background.stringVlaue);
                    }
                } catch (Exception e) {}
                sendFileAndCommandList.removeFirst();
            } else try {
	            Thread.sleep(100);
            } catch (InterruptedException e1) {
	            e1.printStackTrace();
            }
        }
    }

	public String Get_ClientIpAddress() { return clientIpAddress; }
    
    private void Process()
    {
      while(NeedExit()==false)
        {
	        try {
		        Thread.sleep(3000);
	        } catch (InterruptedException e) {
		        e.printStackTrace();
	        }
        }
    }

    Boolean bHaveRequest = false;
    public void Request_Exit()
    {
        if (bHaveRequest) return;
        bHaveRequest = true;

        if (NeedExit()) return;

        MyLog.i("ClientManage Request_Exit.");

        if (clientIpAddress != null)
            if (clientsManage!=null)
                clientsManage.Request_Exit(clientIpAddress);
    }



    Boolean bNeedExit=false;
    boolean NeedExit() { return bNeedExit; }
	
	public void Exit() {
		if (NeedExit()) return;
		bNeedExit=true;
		
		// 关中间服务；
		if (sendFile!=null) sendFile.Exit();
		// 关套接字连接；
		if (tcpCommunication!=null) tcpCommunication.Exit();

        if (sendFileAndCommandList!=null) sendFileAndCommandList.clear();

        clientsManage=null;
        clientIpAddress=null;
        sendFile=null;
        tcpCommunication=null;

        sendFileAndCommandList = null;

        MyLog.i(clientIpAddress+" "+ Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                +   "Line" +Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
        );
	}
}
