package com.cpm.lorealbaautomation.reports.dailyTva;

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 java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class DailyTVAActivity extends AppCompatActivity {
    TextView tvtotalTargets, tvTotalSales, tvKPI, tvTotalAch;
    ArrayList<DailyTVA> dailyTVAS = new ArrayList<>();
    String counter_Id, visit_date, username;
    private SharedPreferences preferences;
    RecyclerView recyclDailyTVa;
    DailyTVA dailyTVA = null;
    Lorealba_Database db;
    Context context;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_tva);
        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);
        validatefilterui();
        dailyTVA();
    }

    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, "");
        recyclDailyTVa = findViewById(R.id.recyclDailyTVa);
        tvtotalTargets = findViewById(R.id.tvtotalTargets);
        tvTotalSales = findViewById(R.id.tvTotalSales);
        tvTotalAch = findViewById(R.id.tvTotalAch);
        tvKPI = findViewById(R.id.tvKPI);

        setTitle(getIntent().getStringExtra(CommonString.Brand) + " Report " + " - " + visit_date);
        db = new Lorealba_Database(context);
        db.open();
    }

    private class DailyTvaAdapter extends RecyclerView.Adapter<DailyTvaAdapter.MyViewHolder> {
        LayoutInflater inflator;
        List<DailyTVA> data;

        public DailyTvaAdapter(Context context, List<DailyTVA> 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_daily_tva, parent, false);
            return new MyViewHolder(view);
        }

        @SuppressLint({"SetTextI18n", "DefaultLocale"})
        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
            final DailyTVA current = data.get(position);
            holder.tvVisitedDate.setText(current.getVisitDate());
            holder.tvVisitedDate.setId(position);
            holder.tvAchPer.setText(current.getPer().toString() + "%");
            holder.tvAchPer.setId(position);
            holder.tvTarget.setText(String.format("%.0f", current.getTarget()));
            holder.tvTarget.setId(position);
            holder.tvTotalSale.setText(current.getTotalSale().toString());
            holder.tvTotalSale.setId(position);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tvVisitedDate, tvTarget, tvTotalSale, tvAchPer;

            public MyViewHolder(View itemView) {
                super(itemView);
                tvVisitedDate = (TextView) itemView.findViewById(R.id.tvVisitedDate);
                tvTotalSale = (TextView) itemView.findViewById(R.id.tvTotalSale);
                tvTarget = (TextView) itemView.findViewById(R.id.tvTarget);
                tvAchPer = (TextView) itemView.findViewById(R.id.tvAchPer);
            }
        }
    }

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

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

    @SuppressLint({"SetTextI18n", "DefaultLocale"})
    private void dailyTVA() {
        db.open();
        dailyTVAS = db.getDailyTVAReport(counter_Id, username);
        dailyTVA = db.getTotalDailyTVA(counter_Id, username);
        if (!dailyTVAS.isEmpty() && dailyTVA != null) {
            tvtotalTargets.setText(getString(R.string.Rs) + " " + String.format("%.0f", dailyTVA.getTarget()));
            tvTotalSales.setText(getString(R.string.Rs) + " " + dailyTVA.getTotalSale());
            tvTotalAch.setText(String.format("%.2f", dailyTVA.getPer()) + "%");
            recyclDailyTVa.setAdapter(new DailyTvaAdapter(context, dailyTVAS));
            recyclDailyTVa.setLayoutManager(new LinearLayoutManager(context));
        }
    }

    @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) -> {}));
    }
}