package com.cpm.lorealbaautomation.reports;

import static com.cpm.lorealbaautomation.constant.CommonFunctions.getFilters;

import android.annotation.SuppressLint;
import android.content.Context;
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.GridLayoutManager;
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.TextView;

import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.adapter.SubAxeValueAdapter;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.gsonGetterSetter.ReportSalesConsumerProductwise;
import com.cpm.lorealbaautomation.gsonGetterSetter.ReportSalesConsumerWise;

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

public class ReportConsumerProductWiseActivity extends AppCompatActivity {
    public ArrayList<ReportSalesConsumerProductwise> reportsList = new ArrayList<>();
    ReportSalesConsumerWise consumerObject = new ReportSalesConsumerWise();
    String counter_Id, visit_date, username, report_name = "";
    private SharedPreferences preferences;
    RecyclerView recycle_reports;
    TextView consumer_name;
    Context context;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reports_consumerproductwise);
        context = this;
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        iduserinterface();

        String couter_code = preferences.getString(CommonString.KEY_COUNTER_CODE, "");
        TextView txt_counter_code = (TextView) findViewById(R.id.txt_counter_code);
        txt_counter_code.setText("Counter Code - " + couter_code + " / Id - " + counter_Id);
    }

    @SuppressLint("SetTextI18n")
    private void iduserinterface() {
       preferences = PrefHelper.getPrefs(context);
        counter_Id = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        username = preferences.getString(CommonString.KEY_USERNAME, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        recycle_reports = (RecyclerView) findViewById(R.id.recycle_reports);
        consumer_name = (TextView) findViewById(R.id.consumer_name);
        report_name = getIntent().getStringExtra(CommonString.Brand);
        setTitle("Reports Consumer Product Wise - " + visit_date);

        if (getIntent().getSerializableExtra(CommonString.TAG_OBJECT) != null) {
            reportsList = (ArrayList<ReportSalesConsumerProductwise>) getIntent().getSerializableExtra(CommonString.TAG_OBJECT);
        }
        if (getIntent().getSerializableExtra(CommonString.KEY_Consumer_Object) != null) {
            consumerObject = (ReportSalesConsumerWise) getIntent().getSerializableExtra(CommonString.KEY_Consumer_Object);
            if (consumerObject != null && consumerObject.getConsumerName() != null) {
                consumer_name.setVisibility(View.VISIBLE);
                consumer_name.setText("Consumer Name : " + consumerObject.getConsumerName() + " - " + consumerObject.getMobileNumber());
            }
        } else {
            consumer_name.setVisibility(View.GONE);
        }

        if (!reportsList.isEmpty()) {
            validatefilterui();
            recycle_reports.setAdapter(new ReportsAdapter(context, reportsList));
            recycle_reports.setLayoutManager(new LinearLayoutManager(context));
        }
    }

    private class ReportsAdapter extends RecyclerView.Adapter<ReportsAdapter.MyViewHolder> {
        LayoutInflater inflator;
        List<ReportSalesConsumerProductwise> data;

        public ReportsAdapter(Context context, List<ReportSalesConsumerProductwise> data) {
            inflator = LayoutInflater.from(context);
            this.data = data;
        }

        @NonNull
        @Override
        public ReportsAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            View view = inflator.inflate(R.layout.row_report_consumer_product_wise, parent, false);
            return new ReportsAdapter.MyViewHolder(view);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(final ReportsAdapter.MyViewHolder holder, final int position) {
            final ReportSalesConsumerProductwise current = data.get(position);

            holder.datewith_day.setText(current.getVisitDate() + " - " + current.getWeekDay());
            holder.datewith_day.setId(position);

            holder.product_name.setText(current.getProductName());
            holder.product_name.setId(position);

            holder.txt_qty.setText(String.valueOf(current.getQty()));
            holder.txt_qty.setId(position);

            holder.txt_salevalue.setText(String.valueOf(current.getSalesValue()));
            holder.txt_salevalue.setId(position);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView datewith_day, product_name, txt_qty, txt_salevalue;
            CardView sale_card;

            public MyViewHolder(View itemView) {
                super(itemView);
                datewith_day = (TextView) itemView.findViewById(R.id.datewith_day);
                product_name = (TextView) itemView.findViewById(R.id.product_name);
                txt_qty = (TextView) itemView.findViewById(R.id.txt_qty);
                txt_salevalue = (TextView) itemView.findViewById(R.id.txt_salevalue);
                sale_card = (CardView) itemView.findViewById(R.id.sale_card);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 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);
            ReportConsumerProductWiseActivity.this.finish();
        }

        return super.onOptionsItemSelected(item);
    }

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

    @SuppressLint("NotifyDataSetChanged")
    private void validatefilterui() {
        RecyclerView rec_subaxe = (RecyclerView) findViewById(R.id.rec_subaxe);
        rec_subaxe.setLayoutManager(new GridLayoutManager(context, 3));
        rec_subaxe.setAdapter(new SubAxeValueAdapter(context, getFilters(), "Sub Axe", true,false, (filterText, placeHolder) -> {}));
    }
}
