package com.cpm.lorealbaautomation.reports;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
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.GridLayoutManager;
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.TextView;

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.reportGetterSetter.ReportTop10Value;
import com.cpm.lorealbaautomation.reports.countervaluevolume.CounterValueVolumeDetailActivity;
import com.cpm.lorealbaautomation.reports.dailyTva.DailyTVAActivity;
import com.cpm.lorealbaautomation.reports.inwarddetails.CounterInwordDetailActivity;
import com.cpm.lorealbaautomation.reports.testerdetails.TesterUsageReportActivity;

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

public class ReportsActivity extends AppCompatActivity {
    public ArrayList<ReportTop10Value> reportsList = new ArrayList<>();
    ReportTop10Value object = new ReportTop10Value();
    String counter_Id, visit_date, username;
    private SharedPreferences preferences;
    RecyclerView recycle_reports;
    String userType = "";
    Lorealba_Database db;
    Context context;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reports);
        context = this;
        db = new Lorealba_Database(context);
        db.open();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        userType = getIntent().getStringExtra("from");
        Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        iduserinterface();
        setTitle("Reports List - " + 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);
    }

    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);
        int count = 2;
        if (CommonFunctions.isTablet(context)) {
            count = 3;
        }
        recycle_reports.setAdapter(new ReportListAdapter(context, addNames()));
        recycle_reports.setLayoutManager(new GridLayoutManager(this, count));
    }

    private ArrayList<ReportTop10Value> addNames() {
        if (!userType.isEmpty() && userType.equalsIgnoreCase(CommonString.TAG_FROM_ME)) {
            object = new ReportTop10Value();
            object.setBrandName("Counter Coverage");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.stock_check);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Sale Reports");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.salesreport);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Sale Reports Day Wise");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.daywise_report);
            reportsList.add(object);

        } else {
            object = new ReportTop10Value();
            object.setBrandName("Stock Ledger Detail");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.stock_ledger);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Counter Inward Stock Detail");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.inward_stock_detail);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Counter Value Detail");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.value_detail);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Counter Volume Detail");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.volume_detail);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Tester Usage Detail");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.tester_usage_detail);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("L3M OffTake");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.sale_growth);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Daily TVA");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.daily_tva);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("KPI Tracker");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.kpi_tracker);
            reportsList.add(object);

            /*
            old report of top 10 value and volume
            object = new ReportTop10Value();
            object.setBrandName("Top 10 Value");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.report_value);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Top 10 Volume");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.report_volume);
            reportsList.add(object);*/

            object = new ReportTop10Value();
            object.setBrandName("Attendance Reports");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.report_attendance);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("BA Availability");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.report_baa);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Inward Lines Summary");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.report_inword);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Tester Stock Summary");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.report_tester);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Damaged Stock Summary");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.report_damage);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Sale Reports");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.salesreport);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Sale Reports Day Wise");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.daywise_report);
            reportsList.add(object);

            object = new ReportTop10Value();
            object.setBrandName("Stock Check Reports");
            object.setColor_code(getResources().getColor(R.color.white));
            object.setDrawble_code(R.mipmap.stock_check);
            reportsList.add(object);
        }

        return reportsList;
    }

    private class ReportListAdapter extends RecyclerView.Adapter<ReportListAdapter.MyViewHolder> {
        LayoutInflater inflator;
        List<ReportTop10Value> data;

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

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

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(final ReportListAdapter.MyViewHolder holder, final int position) {
            final ReportTop10Value current = data.get(position);
            holder.img_icon.setImageResource(current.getDrawble_code());
            holder.img_icon.setId(position);

            holder.report_name_txt.setText(current.getBrandName());
            holder.report_name_txt.setId(position);

            holder.sale_card.setCardBackgroundColor(current.getColor_code());
            holder.sale_card.setId(position);

            holder.sale_card.setOnClickListener(view -> {
                switch (current.getBrandName()) {
                   /* case "Top 10 Value":
                    case "Top 10 Volume":
                        startActivity(new Intent(context, ReportsTop10Value.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;*/
                    case "Inward Lines Summary":
                        startActivity(new Intent(context, ReportsInwordStock.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Attendance Reports":
                        startActivity(new Intent(context, ReportAttendanceSummaryActivity.class));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Tester Stock Summary":
                    case "Stock Check Reports":
                    case "Counter Coverage":
                        startActivity(new Intent(context, TesterReportActivity.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Damaged Stock Summary":
                        startActivity(new Intent(context, DamagedStockHistory.class));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "BA Availability":
                        startActivity(new Intent(context, ReportBaAvailability.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Sale Reports":
                        startActivity(new Intent(context, SalesReportsActivity.class).putExtra(CommonString.KEY_USER_TYPE, userType));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Sale Reports Day Wise":
                        startActivity(new Intent(context, ReportDayWiseActivity.class).putExtra(CommonString.KEY_USER_TYPE, userType));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "KPI Tracker":
                        startActivity(new Intent(context, KPITrackerActivity.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "L3M OffTake":
                        startActivity(new Intent(context, DashbordOffTakeReportActivity.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Daily TVA":
                        startActivity(new Intent(context, DailyTVAActivity.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Tester Usage Detail":
                        startActivity(new Intent(context, TesterUsageReportActivity.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;

                    case "Counter Value Detail":
                    case "Counter Volume Detail":
                        startActivity(new Intent(context, CounterValueVolumeDetailActivity.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                    case "Counter Inward Stock Detail":
                    case "Stock Ledger Detail":
                        startActivity(new Intent(context, CounterInwordDetailActivity.class).putExtra(CommonString.Brand, current.getBrandName()));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        break;
                }
            });
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView report_name_txt;
            CardView sale_card;
            ImageView img_icon;

            public MyViewHolder(View itemView) {
                super(itemView);
                report_name_txt = (TextView) itemView.findViewById(R.id.report_name_txt);
                img_icon = (ImageView) itemView.findViewById(R.id.img_icon);
                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);
            ReportsActivity.this.finish();

        }

        return super.onOptionsItemSelected(item);
    }

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

    }

}
