package aicare.net.cn.sdk.ailinksdkdemoandroid.modules.smart_cycling_table;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.pinwang.ailinkblesdk.modules.smart_cycling_table.SmartCyclingTableViewModel;

import java.util.ArrayList;
import java.util.List;

import aicare.net.cn.sdk.ailinksdkdemoandroid.R;
import aicare.net.cn.sdk.ailinksdkdemoandroid.base.BleNewBaseActivity;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.TimeUtils;

/**
 * 智能码表界面
 *
 * @author xing
 * @date 2025/12/11
 */
public class SmartCyclingTableActivity extends BleNewBaseActivity implements View.OnClickListener {

    private Button btnGetGps, btnPause, btnContinue, btnClear;
    private TextView tvLastData;
    private List<String> mList;
    private ArrayAdapter listAdapter;
    private Context mContext;
    private String mAddress;
    private int mCid;
    private int mVid;
    private int mPid;
    private SmartCyclingTableViewModel mViewModel;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_cycling_table);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        btnGetGps.setOnClickListener(this);
        btnPause.setOnClickListener(this);
        btnContinue.setOnClickListener(this);
        btnClear.setOnClickListener(this);
        if (mViewModel != null) {
            mViewModel.getMGpsLogLiveData().observe(this, s -> {
                addLog(s);
            });

//            mViewModel.getMGpsNumberLiveData().observe(this, map -> {
//                String msg = "";
//                msg += map.get("GPGSV") + "\n";
//                msg += map.get("GBGSV") + "\n";
//                msg += map.get("GAGSV") + "\n";
//                msg += map.get("GLGSV") + "\n";
//                tvLastData.setText(msg);
//            });
            mViewModel.getConnectStatusData().observe(this, status -> {
                switch (status) {

                    case 1:
                        addLog("扫描中");
                        break;
                    case 2:
                        addLog("连接断开");
                        break;
                    case 3:
                        addLog("连接成功");
                        break;

                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnGetGps) {
            mViewModel.getGpsInfo();
        } else if (id == R.id.btnPause) {
            mViewModel.refreshDataPause();
        } else if (id == R.id.btnContinue) {
            mViewModel.refreshDataContinue();
        } else if (id == R.id.btnClear) {
            clear();
        }

    }

    private void initData() {
        mViewModel = new ViewModelProvider(this).get(SmartCyclingTableViewModel.class);

        mContext = this;
        mAddress = getIntent().getStringExtra("mac");
        mCid = getIntent().getIntExtra("cid", -1);
        mVid = getIntent().getIntExtra("vid", -1);
        mPid = getIntent().getIntExtra("pid", -1);
        if (isServiceBinded) {
            onServiceSuccess();
        }
    }

    private void initView() {
        tvLastData = findViewById(R.id.tvLastData);
        btnGetGps = findViewById(R.id.btnGetGps);
        btnPause = findViewById(R.id.btnPause);
        btnContinue = findViewById(R.id.btnContinue);
        btnClear = findViewById(R.id.btnClear);
        mList = new ArrayList<>();
        ListView listView = findViewById(R.id.listview);
        listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        listView.setAdapter(listAdapter);
    }

    private boolean isServiceBinded = false;

    @Override
    public void onServiceSuccess() {
        isServiceBinded = true;
        if (mViewModel != null) {
            mViewModel.init(mAddress, mCid, mVid, mPid);
        }
    }

    @Override
    public void onServiceErr() {

    }

    @Override
    public void unbindServices() {

    }

    private void addLog(String msg) {
        mList.add(TimeUtils.getTimeSSS() + msg);
        listAdapter.notifyDataSetChanged();
    }

    private void clear() {
        mList.clear();
        listAdapter.notifyDataSetChanged();
    }
}
