package com.cpm.lorealbaautomation.reports;

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

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

public class ReportBaAvailability extends AppCompatActivity {
    public ArrayList<ReportBAAvailability> reportsList = new ArrayList<>();
    String counter_Id, visit_date, username, report_name = "";
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor = null;
    RecyclerView recycle_reports;
    Lorealba_Database db;
    Context context;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_ba_availability);
        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);
        editor = preferences.edit();
        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("Reports " + report_name + " - " + visit_date);
        db = new Lorealba_Database(context);
        db.open();
        reportsList = db.getreport_ba_availability();
        if (!reportsList.isEmpty()) {
            recycle_reports.setAdapter(new ReportsAdapter(context, reportsList));
            recycle_reports.setLayoutManager(new LinearLayoutManager(context));
        }
    }

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

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

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

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

            holder.AchievedMandays.setText("" + current.getAchievedMandays());
            holder.AchievedMandays.setId(position);

            holder.BudgetedMandays.setText(current.getBudgetedMandays().toString());
            holder.BudgetedMandays.setId(position);

            holder.TotalTime.setText(current.getTotalTime().toString());
            holder.TotalTime.setId(position);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView date_txt, BudgetedMandays, AchievedMandays, TotalTime;
            CardView sale_card;

            public MyViewHolder(View itemView) {
                super(itemView);
                date_txt = (TextView) itemView.findViewById(R.id.date_txt);
                BudgetedMandays = (TextView) itemView.findViewById(R.id.BudgetedMandays);
                AchievedMandays = (TextView) itemView.findViewById(R.id.AchievedMandays);
                TotalTime = (TextView) itemView.findViewById(R.id.TotalTime);
                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);
            ReportBaAvailability.this.finish();

        }


        return super.onOptionsItemSelected(item);
    }

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

    }
}
