package com.cpm.lorealbaMabeline.dailyactivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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.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.JourneyPlan;
import com.cpm.lorealbaMabeline.gsonGetterSetter.MasterSurvey;

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


public class BASurveyListActivity extends AppCompatActivity {
    List<Integer> checkHeaderArray = new ArrayList<>();
    boolean checkflag = true;
    private SharedPreferences preferences;
    String username, date, counter_id = "", SurveyType;
    Lorealba_Database db;
    Toolbar toolbar;
    RecyclerView BAListView;
    ArrayList<MasterSurvey> listDataHeader;
    Context _context;
    BASurveyListAdapter adapter;
    JourneyPlan jcp_Object = new JourneyPlan();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ba_survey_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);


        BAListView = (RecyclerView) findViewById(R.id.ba_survey_list);

        db = new Lorealba_Database(_context);
        db.open();
        jcp_Object = db.getBIDfromJCP(username, counter_id);
        //Intent data
        if (getIntent().getStringExtra("SurveyType") != null) {
            SurveyType = getIntent().getStringExtra("SurveyType");
        }

        if (SurveyType.equalsIgnoreCase("BA")) {
            db.open();
            listDataHeader = db.getBASurveySavedData("BA", jcp_Object, date);
        } else {
            db.open();
            listDataHeader = db.getBASurveySavedData("Counter", jcp_Object, date);
        }

        if (listDataHeader.isEmpty()) {
            if (SurveyType.equalsIgnoreCase("BA")) {
                db.open();
                listDataHeader = db.getSurveyData("BA");
            } else {
                db.open();
                listDataHeader = db.getSurveyData("Counter");
            }
        }

        adapter = new BASurveyListAdapter(_context, listDataHeader);
        BAListView.setLayoutManager(new LinearLayoutManager(_context));
        BAListView.setAdapter(adapter);

        if (SurveyType.equalsIgnoreCase("BA")) {
            setTitle("BA Survey List - " + username);
        } else {
            setTitle("Counter Survey List - " + date);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent().getStringExtra("SurveyType") != null) {
            SurveyType = getIntent().getStringExtra("SurveyType");
        }
    }

    @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) {
            finish();
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        }

        return super.onOptionsItemSelected(item);
    }

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

    }

    private class BASurveyListAdapter extends RecyclerView.Adapter<BASurveyListAdapter.ViewHolder> {
        Context context;
        ArrayList<MasterSurvey> listDataHeader;

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

        @NonNull
        @Override
        public BASurveyListAdapter.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 BASurveyListAdapter.ViewHolder holder, final int pos) {
            final MasterSurvey object = listDataHeader.get(pos);
            holder.surveyName.setText(object.getSurveyName().trim());
            holder.surveyName.setId(pos);
            db.open();
            if (object.getUploadStatus() == 1) {
                holder.complete_icon.setVisibility(View.VISIBLE);
                holder.complete_icon.setId(pos);
                db.open();
            } else if (db.getSurveyUploadStatus(SurveyType, object.getSurveyId().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 (object.getUploadStatus() == 1) {
                        AlertandMessages.showToastMsg(context, "Survey Already Done");
                    } else if (db.getSurveyUploadStatus(SurveyType, object.getSurveyId().toString(), username, counter_id)) {
                        AlertandMessages.showToastMsg(context, "Survey Already Done");
                    } else {
                        startActivity(new Intent(context, BASurveyActivity.class).putExtra("SurveyId", listDataHeader.get(pos).getSurveyId().toString()).putExtra("SurveyType", SurveyType));
                        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
                        BASurveyListActivity.this.finish();
                    }
                }
            });

        }

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

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

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