package com.cpm.lorealbaMabeline.reports;

import android.content.Context;
import android.content.SharedPreferences;

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.lorealbaMabeline.Database.Lorealba_Database;
import com.cpm.lorealbaMabeline.R;
import com.cpm.lorealbaMabeline.constant.CommonString;
import com.cpm.lorealbaMabeline.delegates.PrefHelper;
import com.cpm.lorealbaMabeline.reportGetterSetter.ReportAttendanceDetail;
import com.cpm.lorealbaMabeline.reportGetterSetter.ReportAttendanceSummary;

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

public class ReportAttendanceSummaryActivity extends AppCompatActivity {
    public ArrayList<ReportAttendanceSummary> reportatt_summaryList = new ArrayList<>();
    ArrayList<ReportAttendanceDetail> report_att_detailList = new ArrayList<>();
    String counter_Id, visit_date, username;
    private SharedPreferences preferences;    private SharedPreferences.Editor editor = null;
    RecyclerView recycle_reports_att_summary, recycle_reports_details;
    Lorealba_Database db;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_attendance_summary);
        context = this;
        db = new Lorealba_Database(context);
        db.open();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        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_att_summary = (RecyclerView) findViewById(R.id.recycle_reports_att_summary);
        recycle_reports_details = (RecyclerView) findViewById(R.id.recycle_reports_details);

        setTitle("Attendance Reports - " + visit_date);
        db = new Lorealba_Database(context);
        db.open();
        reportatt_summaryList = db.getAttendanceSummary();
        if (reportatt_summaryList.size() > 0) {
            recycle_reports_att_summary.setAdapter(new ReportsAttSummaryAdapter(context, reportatt_summaryList));
            recycle_reports_att_summary.setLayoutManager(new LinearLayoutManager(context));
        }
        db.open();
        report_att_detailList = db.getAttendanceDetails();
        if (report_att_detailList.size() > 0) {
            recycle_reports_details.setAdapter(new ReportsAttDetailsAdapter(context, report_att_detailList));
            recycle_reports_details.setLayoutManager(new LinearLayoutManager(context));
        }
    }

    public class ReportsAttSummaryAdapter extends RecyclerView.Adapter<ReportsAttSummaryAdapter.MyViewHolder> {
        private LayoutInflater inflator;
        List<ReportAttendanceSummary> data;

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

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

        @Override
        public void onBindViewHolder(final ReportsAttSummaryAdapter.MyViewHolder holder, final int position) {
            final ReportAttendanceSummary current = data.get(position);

            holder.att_status.setText("" + current.getAttendanceStatus());
            holder.att_status.setId(position);

            holder.last_month_txt.setText("" + current.getLastMonth().toString());
            holder.last_month_txt.setId(position);

            holder.mtd_txt.setText("" + current.getMTD().toString());
            holder.mtd_txt.setId(position);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView att_status, last_month_txt, mtd_txt;
            CardView sale_card;

            public MyViewHolder(View itemView) {
                super(itemView);
                att_status = (TextView) itemView.findViewById(R.id.att_status);
                last_month_txt = (TextView) itemView.findViewById(R.id.last_month_txt);
                mtd_txt = (TextView) itemView.findViewById(R.id.mtd_txt);
                sale_card = (CardView) itemView.findViewById(R.id.sale_card);
            }
        }
    }

    public class ReportsAttDetailsAdapter extends RecyclerView.Adapter<ReportsAttDetailsAdapter.MyViewHolder> {
        private LayoutInflater inflator;
        List<ReportAttendanceDetail> data;

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

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

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            final ReportAttendanceDetail current = data.get(position);
            holder.att_ba_name_txt.setText("" + current.getBAName());
            holder.att_ba_name_txt.setId(position);

            holder.att_date.setText("" + current.getAttDate());
            holder.att_date.setId(position);

            holder.attendance_txt.setText("" + current.getAttendance());
            holder.attendance_txt.setId(position);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView att_ba_name_txt, att_date, attendance_txt;
            CardView sale_card;

            public MyViewHolder(View itemView) {
                super(itemView);
                att_ba_name_txt = (TextView) itemView.findViewById(R.id.att_ba_name_txt);
                att_date = (TextView) itemView.findViewById(R.id.att_date);
                attendance_txt = (TextView) itemView.findViewById(R.id.attendance_txt);
                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);
            ReportAttendanceSummaryActivity.this.finish();

        }


        return super.onOptionsItemSelected(item);
    }

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

    }

}
