package com.example.leudaemialikeme.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leudaemialikeme.Adapter.TimeAdapter;
import com.example.leudaemialikeme.Model.Clock;
import com.example.leudaemialikeme.R;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

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


public class MdctRmdActivity extends BaseActivity {

    private RecyclerView lv;
    public static TimeAdapter timeAdapter;
    private ImageView add_md;
    public static List<Clock> list = new ArrayList<>();
    Context context = MdctRmdActivity.this;


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

        ImageView back;
        back =findViewById(R.id.back_to_index);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MdctRmdActivity.this, MainActivity.class);
                i.putExtra("flag",1);
                startActivity(i);
            }
        });

        add_md = findViewById(R.id.add_md);
        add_md.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(MdctRmdActivity.this, AddClock.class);
                startActivity(intent1);
            }
        });
        lv=(RecyclerView)findViewById(R.id.rv_list_data);
        LitePal.getDatabase();
        initRecycleView();
//        adapter=new TimeAdapter(MdctRmdActivity.this);
//
//
//        lv.setAdapter(adapter);
//        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Switch aSwitch=adapter.getaSwitch(i);
//                if(aSwitch.isChecked()){
//                    aSwitch.setChecked(false);
//                    //进行业务处理
//                }else {
//                    aSwitch.setChecked(true);
//                    //进行业务处理
//                }
//            }
//        });
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        initRecycleView();
    }

    private void initRecycleView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        lv.setLayoutManager(layoutManager);
        timeAdapter = new TimeAdapter(list, context);
        lv.setAdapter(timeAdapter);
        list.clear();
        List<Clock> list1 = LitePal.findAll(Clock.class);
        for (Clock clock : list1) {
            list.add(clock);
        }
        timeAdapter.notifyDataSetChanged();
    }

}

