package cpm.com.gskmtorange.gsk_dailyentry;

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

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.firebase.crashlytics.internal.common.CrashlyticsCore;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;

import cpm.com.gskmtorange.Database.GSKOrangeDB;
import cpm.com.gskmtorange.GetterSetter.OrderReceivedStatus;
import cpm.com.gskmtorange.R;
import cpm.com.gskmtorange.constant.CommonFunctions;
import cpm.com.gskmtorange.constant.CommonString;
import cpm.com.gskmtorange.dailyentry.OrderTakingActivity;
import cpm.com.gskmtorange.xmlGetterSetter.JourneyPlanGetterSetter;

public class OrderConfirmActivity extends AppCompatActivity {
    RecyclerView recyclerview;
    GSKOrangeDB db;
    String visit_date, username, intime;
    private SharedPreferences preferences;
    JourneyPlanGetterSetter jcpGetset;
    Recycler_view_adapter adapter;
    ArrayList<OrderReceivedStatus> inserteslistData;
    private Dialog dialog;
    private boolean ResultFlag;
    Context context;
    LinearLayout poLL;
    TextView sys_gen_poTV;
    String storeid;
    String categoryName="",categoryId="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_confirm);
        context = this;
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        //toolbar.setTitle("Order Status" + "-"+"PO List");
        toolbar.setTitle(context.getString(R.string.order_status_polist));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        declaration();
    }

    private void declaration() {
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        db = new GSKOrangeDB(this);
        db.open();

        //Intent data
        categoryName = getIntent().getStringExtra("categoryName");
        categoryId = getIntent().getStringExtra("categoryId");

        storeid = preferences.getString(CommonString.KEY_STORE_ID, null);
        setDataToListView();
    }

    private void setDataToListView() {
        try {
            db.open();
            inserteslistData=db.getOrderstatusData(storeid,categoryId);
            if(inserteslistData.size()>0){
                adapter = new Recycler_view_adapter(context, inserteslistData);
                recyclerview.setAdapter(adapter);
                recyclerview.setLayoutManager(new LinearLayoutManager(context));
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
        }
    }

    public  class Recycler_view_adapter extends RecyclerView.Adapter<Recycler_view_adapter.Viewholder>{
        ArrayList<OrderReceivedStatus> orderReceivedStatuses;
        Context context;
        public Recycler_view_adapter(Context context, ArrayList<OrderReceivedStatus> list) {
            this.context = context;
            this.orderReceivedStatuses = list;
        }

        @NonNull
        @Override
        public Recycler_view_adapter.Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.order_status_item_layout,viewGroup,false);
            return new Viewholder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull final Recycler_view_adapter.Viewholder viewholder, final int pos) {
            final OrderReceivedStatus current = orderReceivedStatuses.get(pos);
            viewholder.sys_gen_poTV.setText(current.getSys_gen_po());
            viewholder.podateTV.setText(current.getPodate());
            viewholder.storepoTV.setText(current.getStorepo());

            viewholder.lineItemTV.setText( ""+current.getORDERQTY());
            if(current.getStatus().equalsIgnoreCase("CONFIRMED")){
                viewholder.statusTV.setText(context.getString(R.string.confirmed));
            }
            else if(current.getStatus().equalsIgnoreCase("CONFIRM")){
                viewholder.statusTV.setText(context.getString(R.string.confirmed));
            }
            else if(current.getStatus().equalsIgnoreCase("NOT CONFIRMED")){
                viewholder.statusTV.setText(context.getString(R.string.not_confirmed));
            }
            else if(current.getStatus().equalsIgnoreCase("PENDING")){
                viewholder.statusTV.setText(context.getString(R.string.pending));
            }
            else{
                viewholder.statusTV.setText(current.getStatus());
            }

            viewholder.cardview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(OrderConfirmActivity.this,OrderStatusDetailActivity.class).putExtra("categoryId",String.valueOf(current.getCATEGORY_ID()))
                            .putExtra("SYS_PO_NUM",current.getSys_gen_po())
                            .putExtra("PO_NUM",current.getStorepo())
                            .putExtra("PO_DATE",current.getPodate())
                            .putExtra("POID",String.valueOf(current.getPOID()))
                            .putExtra("LINE",String.valueOf(current.getORDERQTY())));
                    overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                }
            });
        }

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

        public class Viewholder extends RecyclerView.ViewHolder {
            TextView sys_gen_poTV,storepoTV,podateTV,lineItemTV,statusTV;
            CardView cardview;
            LinearLayout storepoLL;
            public Viewholder(@NonNull View itemView) {
                super(itemView);
                cardview= (CardView) itemView.findViewById(R.id.cardview);
                storepoLL= itemView.findViewById(R.id.storepoLL);
                sys_gen_poTV= itemView.findViewById(R.id.sys_gen_poTV);
                storepoTV= itemView.findViewById(R.id.storepoTV);
                podateTV= itemView.findViewById(R.id.podateTV);
                lineItemTV= itemView.findViewById(R.id.lineItemTV);
                statusTV= itemView.findViewById(R.id.statusTV);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //return super.onOptionsItemSelected(item);
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        }
        return super.onOptionsItemSelected(item);
    }

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

    @Override
    protected void onResume() {
        super.onResume();
        CommonFunctions.updateLangResources(context, preferences.getString(CommonString.KEY_LANGUAGE, ""));
    }
}