package com.example.surveillanceapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.SharedPreferences;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class ActivityHistory extends AppCompatActivity {

    ArrayList<HistoryList> historyLists;
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager mLayoutManager;
    public TextView line1;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "New Message", Toast.LENGTH_SHORT).show();
            String message = intent.getStringExtra("history");
            line1.setText(message);
            insertItem(line1.getText().toString());
        }
    };
    @Override
    protected void onDestroy() {
        // Unregister since the activity is about to be closed.
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
        super.onDestroy();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        getSupportActionBar().setTitle("List of Messages");
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        line1 = findViewById(R.id.line_1);

        loadData();
        buildRecyclerView();
        setClearButton();


        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("ListOfMessages"));
      //  LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("DateOfMessages"));


        saveData();

        Button buttonSave = findViewById(R.id.button_save);
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
            }
        });
    }

    private void saveData() {
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(historyLists);
        editor.putString("task list", json);
        editor.apply();
    }

    private void loadData() {
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("task list", null);
        Type type = new TypeToken<ArrayList<HistoryList>>() {}.getType();
        historyLists = gson.fromJson(json, type);

        if (historyLists == null) {
            historyLists = new ArrayList<>();
        }
    }

    private void buildRecyclerView() {
        recyclerView = findViewById(R.id.recyclerview);
        mLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setHasFixedSize(true);

        adapter = new MyAdapter(historyLists);
        recyclerView.setAdapter(adapter);
    }

  private void setClearButton() {
        Button buttonInsert = findViewById(R.id.button_insert);
        buttonInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int size= historyLists.size();
                historyLists.clear();
                adapter.notifyItemRangeRemoved(0,size);
            }
        });
    }

    private void insertItem(String line1) {
        historyLists.add(new HistoryList(line1));
        adapter.notifyItemInserted(historyLists.size());
    }


}

