package com.cpm.lorealbaautomation.reports;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.gsonGetterSetter.ReportMESale;

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

public class MeSaleReport extends AppCompatActivity {
    public ArrayList<ReportMESale> meSaleReportArrayList = new ArrayList<>();
    String counter_Id, visit_date, username, report_name = "";
    private SharedPreferences preferences;
    RecyclerView recycle_reports;
    Lorealba_Database db;
    Context context;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_sale_report);
        context = this;
        db = new Lorealba_Database(context);
        db.open();
        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);
    }

    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);
        report_name = getIntent().getStringExtra(CommonString.Brand);
        setTitle("ME_Sale Report " +  " - " + visit_date);
        db = new Lorealba_Database(context);
        db.open();
        meSaleReportArrayList = db.getreportMeSale();

        if (meSaleReportArrayList.size()>0){
            recycle_reports.setAdapter(new ReportsAdapter(context, meSaleReportArrayList));
            recycle_reports.setLayoutManager(new LinearLayoutManager(context));
        }
    }

    public class ReportsAdapter extends RecyclerView.Adapter<ReportsAdapter.MyViewHolder> {
        LayoutInflater inflator;
        List<ReportMESale> data;

        public ReportsAdapter(Context context, List<ReportMESale> 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_reports_me_sale, parent, false);
            return new MyViewHolder(view);
        }

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



            holder.date_txt.setText(current.getVisitDate());
            holder.date_txt.setId(position);

            holder.contercodeTV.setText(current.getCounterCode());
            holder.contercodeTV.setId(position);

            holder.conternameTV.setText(current.getCounterName());
            holder.conternameTV.setId(position);

            holder.totalsaleTV.setText(String.format("%.2f", current.getTotalsale()));
            holder.totalsaleTV.setId(position);

            holder.totalunitsaleTV.setText(String.valueOf(current.getTotalunitsale()));
            holder.totalunitsaleTV.setId(position);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView date_txt, contercodeTV, conternameTV,totalunitsaleTV,totalsaleTV;
            CardView sale_card;

            public MyViewHolder(View itemView) {
                super(itemView);
                date_txt = (TextView) itemView.findViewById(R.id.date_txt);
                contercodeTV = (TextView) itemView.findViewById(R.id.contercodeTV);
                conternameTV = (TextView) itemView.findViewById(R.id.conternameTV);
                totalunitsaleTV = (TextView) itemView.findViewById(R.id.totalunitsaleTV);
                totalsaleTV = (TextView) itemView.findViewById(R.id.totalsaleTV);
                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);
            MeSaleReport.this.finish();
        }
        return super.onOptionsItemSelected(item);
    }

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