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 android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.cpm.lorealbaautomation.Database.Lorealba_Database;
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.reportGetterSetter.ReportAvgUnitsPerBill;

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

public class KPITrackerActivity extends AppCompatActivity {
    ArrayList<ReportAvgUnitsPerBill> dashReportList = new ArrayList<>();
    String counter_Id, visit_date, username;
    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_kpi_tracker);
        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();
        validatefilterui();
    }

    @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);
        setTitle(getIntent().getStringExtra(CommonString.Brand) + " Report " + " - " + visit_date);
        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);
        db = new Lorealba_Database(context);
        db.open();
        dashReportList = db.getDashboardReport(counter_Id);
        if (!dashReportList.isEmpty()) {
            recycle_reports.setAdapter(new KPITrackerAdapter(context, dashReportList));
            recycle_reports.setLayoutManager(new LinearLayoutManager(context));
        }
    }

    private static class KPITrackerAdapter extends RecyclerView.Adapter<KPITrackerAdapter.MyViewHolder> {
        LayoutInflater inflator;
        List<ReportAvgUnitsPerBill> data;

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

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

        @SuppressLint({"SetTextI18n", "DefaultLocale"})
        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
            final ReportAvgUnitsPerBill current = data.get(position);
            //holder.tvRunRate.setText("KPI - (Run Rate - " + current.getRunRate() + " )");
            holder.tvKpiName.setText(current.getKPI());
            holder.tvKpiName.setId(position);
            holder.tvMTDACH.setText(String.format("%.0f", current.getMTDAch()));
            holder.tvMTDACH.setId(position);
            holder.tvAchPer.setText(String.format("%.0f", current.getAchPer()) + "%");
            holder.tvAchPer.setId(position);
            holder.tvMTDTGT.setText(String.format("%.0f", current.getMTDTg()));
            holder.tvMTDTGT.setId(position);
            holder.tvPendingPer.setText(String.format("%.0f", current.getPending()) + "%");
            holder.tvPendingPer.setId(position);
        }

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

       private static class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tvKpiName, tvMTDACH, tvAchPer, tvMTDTGT, tvPendingPer;


            public MyViewHolder(View itemView) {
                super(itemView);
                tvKpiName = (TextView) itemView.findViewById(R.id.tvKpiName);
                tvMTDACH = (TextView) itemView.findViewById(R.id.tvMTDACH);
                tvAchPer = (TextView) itemView.findViewById(R.id.tvAchPer);
                tvMTDTGT = (TextView) itemView.findViewById(R.id.tvMTDTGT);
                tvPendingPer = (TextView) itemView.findViewById(R.id.tvPendingPer);
            }
        }
    }

    @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);
            KPITrackerActivity.this.finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        KPITrackerActivity.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) -> {

        }));
    }
}