package com.example.jinyu_rfid;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.jinyu_rfid.adapter.ConfigurationTableAdapter;
import com.example.jinyu_rfid.base.BaseActivity;
import com.example.jinyu_rfid.been.ConfigurationData;
import com.example.jinyu_rfid.been.ConfigurationTable;
import com.example.jinyu_rfid.callback.ItemClickCall;
import com.example.jinyu_rfid.databinding.ActivityConfigurationTableBinding;

import org.litepal.LitePal;

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

public class ConfigurationTableActivity extends BaseActivity {
    private ConfigurationTableAdapter adapter;

    private List<ConfigurationData> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityConfigurationTableBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_configuration_table);
        binding.setTitle(getString(R.string.activity_name_configuration));
        adapter = new ConfigurationTableAdapter(this);
        var stringArray = getResources().getStringArray(R.array.project_list);
        var length = stringArray.length;
        dataList = new ArrayList<>(length);
        for (int i = 0; i < length; i++) {
            dataList.add(new ConfigurationData(i, stringArray[i], findStateByIndex(i)));
        }
        adapter.setList(dataList);
        binding.setAdapter(adapter);
    }


/*    public void configurationAddItem(View view) {

    }*/

    public void configurationSave(View view) {
        Log.e("TAG", "configurationSave:" + dataList.toString());
        LitePal.deleteAll(ConfigurationTable.class);
        dataList.forEach(t->{
            ConfigurationTable configurationTable=new ConfigurationTable();
            configurationTable.setIndex(t.getIndex());
            configurationTable.setState(t.isState());
            configurationTable.save();
        });
        var intent = new Intent(this, WriteUserActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
        finish();
    }

    private boolean findStateByIndex(int index) {
        var list = LitePal.where("index = "+ index).limit(1).find(ConfigurationTable.class);
        if (list==null||list.isEmpty()){
            return false;
        }
        return list.get(0).isState();
    }

}