package com.example.upbot;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import com.example.upbot.MSG.MSG_CMD;


@SuppressLint("StaticFieldLeak")
public class ClientActivity extends AppCompatActivity {
    private static EditText msgET;
    private static Button confirmBtn;
    private static Button sendBtn;
    private static EditText ipET;

    private static ImageView startRobot;

    private static ImageView imageView4;

    private static ImageView iv_charge;

    private static ImageView iv_stop_robot;

    private static StringBuffer stringBuffer = new StringBuffer();
    private static TextView msgTV;

    private static final String TAG="WifiDemoLogClientActivity";

    @SuppressLint("HandlerLeak")
    public static Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){
                stringBuffer.append(msg.obj);
                stringBuffer.append("\n");
                msgTV.setText(stringBuffer.toString());
            }
        }
    };

    @Override
    protected   void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
        Toolbar toolbar = findViewById(R.id.toolBar_client);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        Drawable overflowIcon = ContextCompat.getDrawable(this, R.drawable.baseline_more_vert_24);
        if (overflowIcon != null) {
            toolbar.setOverflowIcon(overflowIcon);
        }
        initView();
        setListener();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void initView() {
        ipET = (EditText) findViewById(R.id.ipET);
        msgET = (EditText) findViewById(R.id.msgET);
        sendBtn =  findViewById(R.id.btn_send);
        confirmBtn =  findViewById(R.id.btn_confirm);
        msgTV = (TextView) findViewById(R.id.msgTV);
        startRobot = (ImageView) findViewById(R.id.start_robot_iv);
        imageView4 = (ImageView) findViewById(R.id.imageView4);
        iv_charge = (ImageView) findViewById(R.id.iv_charge);
        iv_stop_robot = (ImageView) findViewById(R.id.iv_stop_robot);
    }

    private  void setListener() {
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!UPbotComm.getInstance().getConnectStatus()) {
                    Toast.makeText(ClientActivity.this, "连接失败无法发送", Toast.LENGTH_SHORT).show();
                    return;
                }
                UPbotComm.getInstance().sendMessage(msgET.getText().toString());
            }
        });
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!UPbotComm.getInstance().connect(ipET.getText().toString())) {
                    Toast.makeText(ClientActivity.this, "未进行连接", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(ClientActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                }
                confirmBtn.setEnabled(false);//连接只点一次
            }
        });
        startRobot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                byte start = MSG_CMD.START_ROBOT;
                if(!UPbotComm.getInstance().getConnectStatus()) {
                    Toast.makeText(ClientActivity.this, "连接失败无法启动", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendCmdMsg(start,"正在启动机器人");
            }
        });
        imageView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                byte value = MSG_CMD.START_COVER_SERVICE;
                if(!UPbotComm.getInstance().getConnectStatus()) {
                    Toast.makeText(ClientActivity.this, "连接失败无法覆盖", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendCmdMsg(value,"正则启动全覆盖割草");
            }
        });
        iv_charge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                byte value = MSG_CMD.CHARGING;
                if(!UPbotComm.getInstance().getConnectStatus()) {
                    Toast.makeText(ClientActivity.this, "连接失败无法充电", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendCmdMsg(value,"正在返回充电");
            }
        });
        iv_stop_robot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                byte value = MSG_CMD.STOP_MOWING;
                if(!UPbotComm.getInstance().getConnectStatus()) {
                    Toast.makeText(ClientActivity.this, "连接失败无法停止", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendCmdMsg(value,"正在停止机器人");
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void sendCmdMsg(byte cmd,String toast){
        byte[] result = SendMsg.sendCmdMsg(cmd);
        UPbotComm.getInstance().sendMessage(result);
        Toast.makeText(ClientActivity.this, toast, Toast.LENGTH_SHORT).show();
    }
}