package com.cpm.lorealbaMabeline.makeupexpert;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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.ImageView;
import android.widget.TextView;

import com.cpm.lorealbaMabeline.Database.Lorealba_Database;
import com.cpm.lorealbaMabeline.R;
import com.cpm.lorealbaMabeline.constant.CommonFunctions;
import com.cpm.lorealbaMabeline.constant.CommonString;
import com.cpm.lorealbaMabeline.delegates.PrefHelper;
import com.cpm.lorealbaMabeline.gsonGetterSetter.InvoiceGetterSetter;

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

public class MakeoverActivity extends AppCompatActivity implements View.OnClickListener {
    String CounterId, visit_date, username;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor = null;
    ArrayList<InvoiceGetterSetter> invoice_List = new ArrayList<>();
    ImageView btn_saletra_add_performa;
    TextView count_cst;
    Lorealba_Database db;
    Context context;
    RecyclerView sale_tracking_recycle;
    boolean clicked_flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_makeover);
        context = this;
        db = new Lorealba_Database(context);
        db.open();
        iduserinterface();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (!CommonFunctions.isTablet(context)) {
            toolbar.setTitleTextAppearance(context, R.style.changestext_sizefor_mobile);
        }
        setTitle("Makeover Capture -" + username);
    }

    private void iduserinterface() {
       preferences = PrefHelper.getPrefs(context);
        editor = preferences.edit();
        CounterId = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        username = preferences.getString(CommonString.KEY_USERNAME, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        btn_saletra_add_performa = (ImageView) findViewById(R.id.btn_saletra_add_performa);
        sale_tracking_recycle = (RecyclerView) findViewById(R.id.sale_tracking_recycle);
        count_cst = (TextView) findViewById(R.id.count_cst);

        btn_saletra_add_performa.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        db = new Lorealba_Database(context);
        db.open();
        invoice_List = db.getMakeoverCaptureByUserMobile(0, null, visit_date, CounterId, username);
        if (invoice_List.size() > 0) {
            sale_tracking_recycle.setAdapter(new ValueAdapter(context, invoice_List));
            sale_tracking_recycle.setLayoutManager(new LinearLayoutManager(context));
        }
        db.open();
        count_cst.setText("" + db.getMakeover(visit_date, CounterId, null,null,false).size());
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_saletra_add_performa) {
            startActivity(new Intent(context, MakeoverDetailsActivity.class));
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        }
    }


    @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) {
            if (clicked_flag) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(CommonString.ONBACK_ALERT_MESSAGE).setCancelable(false).setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    DialogInterface dialog, int id) {
                                overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
                                MakeoverActivity.this.finish();
                            }
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            } else {
                overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
                MakeoverActivity.this.finish();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (clicked_flag) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(CommonString.ONBACK_ALERT_MESSAGE).setCancelable(false).setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(
                                DialogInterface dialog, int id) {
                            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
                            MakeoverActivity.this.finish();
                        }
                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else {
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
            MakeoverActivity.this.finish();
        }
    }


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

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

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

        @Override
        public void onBindViewHolder(final ValueAdapter.MyViewHolder holder, final int position) {
            final InvoiceGetterSetter current = data.get(position);
            if (current.isPos_sale_flag()) {
                holder.pos_icon.setVisibility(View.GONE);
                holder.pos_icon.setId(position);
            } else {
                holder.pos_icon.setVisibility(View.VISIBLE);
                holder.pos_icon.setId(position);
            }

            holder.sale_trcking_name.setText(current.getCustomer_name() + "  - Mobile No. :  " + current.getMobile_no());
            holder.sale_trcking_name.setId(position);

            holder.look_created.setText("Look Created :  " + current.getLook_creater_str());
            holder.look_created.setId(position);

            holder.sale_card.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    db.open();
                    if (!current.isPos_sale_flag()) {
                        startActivity(new Intent(context, MakeoverDetailsActivity.class)
                                .putExtra(CommonString.TAG_OBJECT, db.getMakeoverCaptureByUserMobile(current.getReccept_count(), current.getMobile_no(), visit_date, CounterId, current.getUserName()))
                                .putExtra(CommonString.Key_For_Consumer_Update, "1"));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    } else {
                        startActivity(new Intent(context, MakeoverDetailsActivity.class).putExtra(CommonString.TAG_OBJECT,
                                db.getMakeoverCaptureByUserMobile(current.getReccept_count(), current.getMobile_no(), visit_date, CounterId, current.getUserName()))
                                .putExtra(CommonString.Key_For_Consumer_Update, "0"));
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);

                    }
                }
            });
        }


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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView sale_trcking_name, look_created;
            CardView sale_card;
            ImageView pos_icon;

            public MyViewHolder(View itemView) {
                super(itemView);
                sale_card = (CardView) itemView.findViewById(R.id.sale_card);
                sale_trcking_name = (TextView) itemView.findViewById(R.id.sale_trcking_name);
                look_created = (TextView) itemView.findViewById(R.id.look_created);
                pos_icon = (ImageView) itemView.findViewById(R.id.pos_icon);
            }
        }
    }
}
