package com.cpm.lorealbaautomation.consumeractivity;

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

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

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.LinearLayout;
import android.widget.TextView;

import com.google.firebase.crashlytics.FirebaseCrashlytics;
import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonFunctions;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.gsonGetterSetter.InvoiceGetterSetter;
import com.cpm.lorealbaautomation.printer.PrinterActivity;

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

public class ConsumerReturnsHistoryActivity extends AppCompatActivity {
    public ArrayList<InvoiceGetterSetter> headerList = new ArrayList<>();
    String counter_Id, visit_date, username;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor = null;
    RecyclerView recycl_history;
    Lorealba_Database db;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumer_returns_history);
        context = this;
        db = new Lorealba_Database(context);
        db.open();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (!CommonFunctions.isTablet(context)) {
            toolbar.setTitleTextAppearance(context, R.style.changestext_sizefor_mobile);
        }

        iduserinterface();

    }

    private void iduserinterface() {
       preferences = PrefHelper.getPrefs(context);
        editor = preferences.edit();
        counter_Id = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        username = preferences.getString(CommonString.KEY_USERNAME, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        recycl_history = (RecyclerView) findViewById(R.id.recycl_history);
        db = new Lorealba_Database(context);
        db.open();

        setTitle("Returns History - " + username);

        setDataToListView();
    }


    public void setDataToListView() {
        try {
            db.open();
            headerList = db.getsaleReturnHeaderData(visit_date, counter_Id, username, false, false, null);
            if (headerList.size() > 0) {
                Collections.reverse(headerList);
                recycl_history.setAdapter(new ValueAdapter(context, headerList));
                recycl_history.setLayoutManager(new LinearLayoutManager(context));
            }

        } catch (Exception e) {
            FirebaseCrashlytics.getInstance().recordException(e);
            //test("Exception when fetching", e.toString());
        }
    }

    public class ValueAdapter extends RecyclerView.Adapter<ValueAdapter.MyViewHolder> {
        private LayoutInflater inflator;
        List<InvoiceGetterSetter> data;


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

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

        @Override
        public void onBindViewHolder(final ValueAdapter.MyViewHolder holder, final int position) {
            final InvoiceGetterSetter current = data.get(position);

            holder.txt_name_with_mobile.setText("Consumer Name : " + current.getCustomer_name() + "  Mobile No. : " + current.getMobile_no());
            holder.txt_name_with_mobile.setId(position);


            holder.text_bill_no.setText("Bill No. : " + current.getConsumer_return_bill_no());
            holder.text_bill_no.setId(position);
            db.open();
            ////////////////////get return list from db user and bill wise
            holder.return_productList = db.getsalesReturnChildData(current.getReccept_count(), visit_date, counter_Id, username, false);
            ////////////////////get replace product list from db user and bill wise
            db.open();
            holder.replace_productList = db.getsalesReturnReplaceProduct(current.getReccept_count(), visit_date, counter_Id, username, false);

            if (holder.return_productList.size() > 0) {
                holder.sku_list_returns.setAdapter(new ReturnsSkuAdapter(context, holder.return_productList));
                holder.sku_list_returns.setLayoutManager(new LinearLayoutManager(context));
            }


            if (holder.replace_productList.size() > 0) {
                holder.sku_list_replace_product.setAdapter(new ReturnsSkuAdapter(context, holder.replace_productList));
                holder.sku_list_replace_product.setLayoutManager(new LinearLayoutManager(context));
                holder.rl_replace_product.setVisibility(View.VISIBLE);
                holder.rl_replace_product.setId(position);
            } else {
                holder.rl_replace_product.setVisibility(View.GONE);
                holder.rl_replace_product.setId(position);
            }


            holder.img_printer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(context, PrinterActivity.class).putExtra(CommonString.TAG_OBJECT, holder.return_productList).putExtra(CommonString.TAG_HEADER_OBJECT, current).putExtra(CommonString.TAG_FOR_RETURN, "1"));
                }
            });

        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView txt_name_with_mobile, text_bill_no;
            RecyclerView sku_list_returns, sku_list_replace_product;
            LinearLayout rl_printer, rl_replace_product;
            ImageView img_printer;
            ArrayList<InvoiceGetterSetter> return_productList, replace_productList;

            public MyViewHolder(View itemView) {
                super(itemView);
                txt_name_with_mobile = (TextView) itemView.findViewById(R.id.txt_name_with_mobile);
                text_bill_no = (TextView) itemView.findViewById(R.id.text_bill_no);
                img_printer = (ImageView) itemView.findViewById(R.id.img_printer);
                rl_printer = (LinearLayout) itemView.findViewById(R.id.rl_printer);
                sku_list_returns = (RecyclerView) itemView.findViewById(R.id.sku_list_returns);
                rl_replace_product = (LinearLayout) itemView.findViewById(R.id.rl_replace_product);
                sku_list_replace_product = (RecyclerView) itemView.findViewById(R.id.sku_list_replace_product);
            }
        }
    }


    public class ReturnsSkuAdapter extends RecyclerView.Adapter<ReturnsSkuAdapter.MyViewHolder> {
        private LayoutInflater inflator;
        List<InvoiceGetterSetter> data;

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

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

        @Override
        public void onBindViewHolder(final ReturnsSkuAdapter.MyViewHolder holder, final int position) {
            final InvoiceGetterSetter current = data.get(position);

            holder.product_name.setText("Product : " + current.getProduct());
            holder.product_name.setId(position);

            holder.product_rate.setText("QTY - " + current.getQuantity() + " Rate - " + current.getProduct_rate());
            holder.product_rate.setId(position);

            int total_ammount = Integer.parseInt(current.getQuantity()) * Integer.parseInt(current.getProduct_rate());
            holder.total_ammount_txt.setText("Total Amount - " + total_ammount);
            holder.total_ammount_txt.setId(position);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView product_name, product_rate, total_ammount_txt;

            public MyViewHolder(View itemView) {
                super(itemView);
                product_name = (TextView) itemView.findViewById(R.id.txt_name_with_mobile);
                product_rate = (TextView) itemView.findViewById(R.id.text_bill_no);
                total_ammount_txt = (TextView) itemView.findViewById(R.id.total_ammount_txt);
            }
        }
    }

    @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);
            ConsumerReturnsHistoryActivity.this.finish();
        }


        return super.onOptionsItemSelected(item);
    }

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

    }
}
