package com.cpm.lorealbaMabeline.dailyactivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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.lorealbaMabeline.Database.Lorealba_Database;
import com.cpm.lorealbaMabeline.R;
import com.cpm.lorealbaMabeline.constant.AlertandMessages;
import com.cpm.lorealbaMabeline.constant.CommonFunctions;
import com.cpm.lorealbaMabeline.constant.CommonString;
import com.cpm.lorealbaMabeline.delegates.PrefHelper;
import com.cpm.lorealbaMabeline.gsonGetterSetter.MEMEAuditQuestion;
import com.cpm.lorealbaMabeline.makeupexpert.MEQuizActivity;

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

public class MeQuizListActivity extends AppCompatActivity {
    List<Integer> checkHeaderArray = new ArrayList<>();
    boolean checkflag = true;
    private SharedPreferences preferences;
    String username, date, counter_id = "";
    Lorealba_Database db;
    Toolbar toolbar;
    RecyclerView me_quiz_list;
    ArrayList<MEMEAuditQuestion> listDataHeader = new ArrayList<>();
    Context _context;
    MEQuizListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_quiz_list);
        _context = this;
        preferences = PrefHelper.getPrefs(this);
        date = preferences.getString(CommonString.KEY_DATE, null);
        username = preferences.getString(CommonString.KEY_USERNAME, null);
        counter_id = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        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("ME Quiz List - " + username);
        me_quiz_list = (RecyclerView) findViewById(R.id.me_quiz_list);
        db = new Lorealba_Database(_context);
        db.open();

        listDataHeader = db.getMeQuiz();
        if (listDataHeader.size()>0){

            me_quiz_list.setAdapter(new MEQuizListAdapter(_context,listDataHeader));
            me_quiz_list.setLayoutManager(new LinearLayoutManager(_context));
        }
    }

    private class MEQuizListAdapter extends RecyclerView.Adapter<MEQuizListAdapter.ViewHolder> {
        Context context;
        ArrayList<MEMEAuditQuestion> listDataHeader;

        public MEQuizListAdapter(Context context, ArrayList<MEMEAuditQuestion> listDataHeader) {
            this.context = context;
            this.listDataHeader = listDataHeader;
        }

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

        @Override
        public void onBindViewHolder(@NonNull MEQuizListAdapter.ViewHolder holder, final int pos) {
            final MEMEAuditQuestion object = listDataHeader.get(pos);

            holder.quiz_name.setText(object.getQuizName().trim() + " ID - " + object.getQuizId().toString());
            holder.quiz_name.setId(pos);
            db.open();
            if (db.getme_quiz(username, date, counter_id, object.getQuizId().toString()).size() > 0) {
                holder.complete_icon.setVisibility(View.VISIBLE);
                holder.complete_icon.setId(pos);
                db.open();
            } else if (db.getMeQuizUploadStatus(object.getQuizId().toString(), username,counter_id)) {
                holder.complete_icon.setVisibility(View.VISIBLE);
                holder.complete_icon.setId(pos);
            } else {
                holder.complete_icon.setVisibility(View.GONE);
                holder.complete_icon.setId(pos);
            }

            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    db.open();
                    if (db.getme_quiz(username, date, counter_id, object.getQuizId().toString()).size() > 0) {
                        AlertandMessages.showToastMsg(context, "ME Quiz Already Done");
                        db.open();
                    } else if (db.getMeQuizUploadStatus(object.getQuizId().toString(), username,counter_id)) {
                        AlertandMessages.showToastMsg(context, "ME Quiz Already Done");
                    } else {
                        startActivity(new Intent(context, MEQuizActivity.class).putExtra(CommonString.TAG_OBJECT, object));
                        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
                        MeQuizListActivity.this.finish();
                    }
                }
            });

        }

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

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView quiz_name;
            CardView cardView;
            ImageView complete_icon;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                quiz_name = (TextView) itemView.findViewById(R.id.survey_name);
                cardView = (CardView) itemView.findViewById(R.id.survey_view);
                complete_icon = (ImageView) itemView.findViewById(R.id.complete_img);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
            MeQuizListActivity.this.finish();
        }

        return super.onOptionsItemSelected(item);
    }

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

    }
}
