package com.cpm.lorealbaautomation.dailyactivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonFunctions;
import com.cpm.lorealbaautomation.gsonGetterSetter.NotificationData;
import com.cpm.lorealbaautomation.gsonGetterSetter.NotificationsGetterSetter;

import java.util.ArrayList;


public class NotificationsActivity extends AppCompatActivity {
    NotificationsGetterSetter object = null;
    Context context;
    Lorealba_Database db;
    RecyclerView notification_view;
    NotificationAdapter mAdapter;
    ArrayList<NotificationData> notificationList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notifications);
        context = this;
        db = new Lorealba_Database(context);
        db.open();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (!CommonFunctions.isTablet(context)) {
            toolbar.setTitleTextAppearance(context, R.style.changestext_sizefor_mobile);
        }

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("Notifications");
        notification_view = (RecyclerView) findViewById(R.id.notification_view);
    }


    @Override
    protected void onResume() {
        super.onResume();
        createNotificationView();
    }

    private void createNotificationView() {
        db.open();
        notificationList = db.getNotificationList();
        if (notificationList.size() > 0) {
            mAdapter = new NotificationAdapter(notificationList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            notification_view.setLayoutManager(mLayoutManager);
            notification_view.setAdapter(mAdapter);
        }
    }

    public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.ViewHolder> {

        ArrayList<NotificationData> notificationList = new ArrayList<>();

        public NotificationAdapter(ArrayList<NotificationData> notificationList) {
            this.notificationList = notificationList;
        }

        @NonNull
        @Override
        public NotificationAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notification_list, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull NotificationAdapter.ViewHolder viewHolder, final int position) {

            viewHolder.title_txt.setText(notificationList.get(position).getTitle());
            viewHolder.body_txt.setText(notificationList.get(position).getBody());
            viewHolder.title_date.setText(notificationList.get(position).getVisited_date());

            if(notificationList.get(position).getRead_status().equalsIgnoreCase("0")){
                viewHolder.notificationView.setCardBackgroundColor(getResources().getColor(R.color.white));
                viewHolder.title_txt.setTextColor(getResources().getColor(R.color.blue));
            }else{
                viewHolder.notificationView.setCardBackgroundColor(getResources().getColor(R.color.gray));
                viewHolder.title_txt.setTextColor(getResources().getColor(R.color.black));
            }

            if (notificationList.get(position).getType() != null) {
                viewHolder.icon_image.setVisibility(View.VISIBLE);
                if (notificationList.get(position).getType().equalsIgnoreCase("image")){
                    viewHolder.icon_image.setBackgroundResource(R.mipmap.image);
                }else if(notificationList.get(position).getType().equalsIgnoreCase("video")) {
                    viewHolder.icon_image.setBackgroundResource(R.mipmap.video);
                } else{
                    viewHolder.icon_image.setBackgroundResource(R.mipmap.text);
                }
            }else {
                viewHolder.icon_image.setBackgroundResource(R.mipmap.text);
            }

            viewHolder.notificationView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    db.open();
                    //db.updateNotificationDetails("1",notificationList.get(position).getId());
                    Intent in = new Intent(getApplicationContext(), NotificationDetailActivity.class);
                    in.putExtra("type", notificationList.get(position).getType());
                    in.putExtra("Title", notificationList.get(position).getTitle());
                    in.putExtra("Date", notificationList.get(position).getVisited_date());
                    in.putExtra("Body", notificationList.get(position).getBody());
                    in.putExtra("Path", notificationList.get(position).getPath());
                    in.putExtra("KEY_ID", notificationList.get(position).getId());
                    startActivity(in);
                }
            });
        }

        @Override
        public int getItemCount() {
            return notificationList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView title_txt, body_txt,title_date;
            ImageView icon_image;
            CardView notificationView;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                title_txt  = (TextView) itemView.findViewById(R.id.title_txt);
                title_date = (TextView) itemView.findViewById(R.id.title_date);
                icon_image =(ImageView)itemView.findViewById(R.id.icon_image);
                body_txt   = (TextView) itemView.findViewById(R.id.body_txt);
                notificationView = (CardView) itemView.findViewById(R.id.card_notification_view);

            }
        }
    }



    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == android.R.id.home) {
            // NavUtils.navigateUpFromSameTask(this);
            finish();
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);

           /* AlertDialog.Builder builder = new AlertDialog.Builder(NotificationsActivity.this);
            builder.setMessage(CommonString.ONBACK_ALERT_MESSAGE)
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            finish();
                            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();*/
        }
        return super.onOptionsItemSelected(item);
    }

}
