package com.mogo.cloud.netty.client;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mogo.cloud.R;
import com.mogo.cloud.netty.LogBean;
import com.mogo.telematic.MogoProtocolMsg;
import com.mogo.telematic.NSDNettyManager;
import com.mogo.telematic.client.listener.NettyClientListener;
import com.mogo.telematic.client.status.ConnectState;

import java.util.Arrays;

import io.netty.channel.Channel;

public class NettyClientActivity extends AppCompatActivity implements View.OnClickListener, NettyClientListener<MogoProtocolMsg> {

    private static final String TAG = "NettyClientActivity";

    private Button mClearLog;
    private Button mSendBtn;
    private Button mConnect;
    private EditText mSendET;
    private RecyclerView mSendList;
    private RecyclerView mReceList;
    private EditText mInputIp;

    private LogAdapter mSendLogAdapter = new LogAdapter();
    private LogAdapter mReceLogAdapter = new LogAdapter();

    private final byte[] sendByte =  new byte[]{0x55, 0x54, 0x72, 0x21};

    private final MogoProtocolMsg mogoProtocolMsg = new MogoProtocolMsg(MogoProtocolMsg.NORMAL_DATA, 3, new byte[]{0x11, 0x12, 0x13});

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_netty_client);
        findViews();
        initView();
        NSDNettyManager.getInstance().searchAndConnectServer(this, "0", true,this);
    }

    private void initView() {
        LinearLayoutManager manager1 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mSendList.setLayoutManager(manager1);
        mSendList.setAdapter(mSendLogAdapter);

        LinearLayoutManager manager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mReceList.setLayoutManager(manager2);
        mReceList.setAdapter(mReceLogAdapter);
    }

    private void findViews() {
        mSendList = findViewById(R.id.send_list);
        mReceList = findViewById(R.id.rece_list);
        mSendET = findViewById(R.id.send_et);
        mConnect = findViewById(R.id.connect);
        mSendBtn = findViewById(R.id.send_btn);
        mClearLog = findViewById(R.id.clear_log);
        mInputIp = findViewById(R.id.et_ip);

        mConnect.setOnClickListener(this);
        mSendBtn.setOnClickListener(this);
        mClearLog.setOnClickListener(this);

        mSendET.setText(Arrays.toString(mogoProtocolMsg.getBody()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.connect:
//                connectNettyServer(mInputIp.getText().toString().trim(), Const.TCP_PORT);
                break;

            case R.id.send_btn:
                if (!NSDNettyManager.getInstance().getConnectStatus()) {
                    Toast.makeText(getApplicationContext(), "未连接,请先连接", Toast.LENGTH_SHORT).show();
                } else {
                    NSDNettyManager.getInstance().sendMogoProtocolMsgToServer(mogoProtocolMsg, isSuccess -> {
                        if (isSuccess) {
                            Log.d(TAG, "Write auth successful");
                            logSend(Arrays.toString(mogoProtocolMsg.getBody()));
                        } else {
                            Log.d(TAG, "Write auth error");
                        }
                    });
                    mSendET.setText(Arrays.toString(mogoProtocolMsg.getBody()));
                }
                break;

            case R.id.clear_log:
                mReceLogAdapter.getDataList().clear();
                mSendLogAdapter.getDataList().clear();
                mReceLogAdapter.notifyDataSetChanged();
                mSendLogAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void onMessageResponseClient(MogoProtocolMsg msg, String sign, Channel channel) {
        String result = msg.toString();
        Log.e(TAG, "onMessageResponse:" + result);
        logRece(result);
    }

    @Override
    public void onClientStatusConnectChanged(final int statusCode, final String sign, Channel channel) {
        runOnUiThread(() -> {
            if (statusCode == ConnectState.STATUS_CONNECT_SUCCESS) {
                Log.e(TAG, "STATUS_CONNECT_SUCCESS:");
                mConnect.setText("DisConnect:" + sign);
            } else {
                Log.e(TAG, "onServiceStatusConnectChanged:" + statusCode);
                mConnect.setText("Connect:" + sign);
            }
        });

    }

    private void logSend(String log) {
        runOnUiThread(() -> {
            LogBean logBean = new LogBean(System.currentTimeMillis(), log);
            mSendLogAdapter.getDataList().add(0, logBean);
            mSendLogAdapter.notifyDataSetChanged();
        });
    }

    private void logRece(String log) {
        runOnUiThread(() -> {
            LogBean logBean = new LogBean(System.currentTimeMillis(), log);
            mReceLogAdapter.getDataList().add(0, logBean);
            mReceLogAdapter.notifyDataSetChanged();
        });
    }

    public void disconnect(View view) {
        NSDNettyManager.getInstance().disconnect();
    }
}
