package com.baoshi.swms.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.baoshi.swms.R;
import com.baoshi.swms.bean.User;
import com.baoshi.swms.net.build.WebSocketClient;
import com.baoshi.swms.net.constant.SwmsWebSocketConstant;
import com.baoshi.swms.net.listener.SwmsWebSocketListener;
import com.baoshi.swms.util.QRCode;

/**
 * 计件宝 ACTIVITY
 *
 * @author Zhendong Zhou
 * @date   2021/12/3 18:04
 */
public class PieceworkActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String BASE_URL = "https://baoshi56.com/employee_qr/qr_code=";
    private WebSocketClient webSocketClient;
    private Handler handler;
    private TextView num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_piecework);
        init();

        num = findViewById(R.id.num);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //启动socket刷新数量
        startWebSocket();
    }

    @Override
    protected void onPause() {
        super.onPause();
        closeSocket();
    }

    @SuppressLint("SetTextI18n")
    private void init() {
        User user = User.getLocalUser();
        ImageView imageView = findViewById(R.id.QRCode);
        Bitmap bitmap = QRCode.createQRCodeBitmap(BASE_URL+user.getId().toString(),200,200);
        imageView.setImageBitmap(bitmap);
        ImageView back = findViewById(R.id.back);
        TextView name = findViewById(R.id.user_name);
        name.setText(name.getText()+User.getLocalUser().getName());


        back.setOnClickListener(this);
    }

    @SuppressLint({"NonConstantResourceId", "SetTextI18n"})
    @Override
    public void onClick(View v) {
        finish();
    }

    public void startWebSocket() {
        if (webSocketClient == null) {
            handler();
            webSocketClient = new WebSocketClient();
        }
        webSocketClient.start(new SwmsWebSocketListener(handler));
    }
    public void closeSocket() {
        webSocketClient.close();
        webSocketClient = null;
    }

    private void handler(){
        handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                if (webSocketClient==null){
                    return;
                }
                super.handleMessage(msg);
                String status = (String) msg.obj;
                //如果SOCKET监听因为一些原因被关闭，通过消息获取此通知，尝试重启
                if (status.equals(SwmsWebSocketConstant.CLOSE_SIGN)){
                    Toast.makeText(PieceworkActivity.this,"服务器连接失败",Toast.LENGTH_SHORT).show();
                    //socketclient重新设置监听
                    startWebSocket();
                }
                if (status.equals(SwmsWebSocketConstant.PULL_SIGN)){
                    int amount = msg.getData().getInt("number");
                    if (num.getText().toString().isEmpty() || Integer.parseInt((String) num.getText()) != amount){
                        num.setText(String.valueOf(amount));
                    }
                }
            }
        };
    }
}