package com.pixelnx.localhelp.ui.user;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.StringRequestListener;
import com.pixelnx.localhelp.R;
import com.pixelnx.localhelp.model.ModelOrderListData;
import com.pixelnx.localhelp.model.ModelUserData;
import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.ProjectUtils;
import com.pixelnx.localhelp.utils.sharedpref.SharedPref;
import com.pixelnx.localhelp.utils.widgets.CustomEditText;
import com.pixelnx.localhelp.utils.widgets.CustomTextMedium;
import com.pixelnx.localhelp.utils.widgets.CustomTextSemiBold;

import org.json.JSONException;
import org.json.JSONObject;

public class ActivityRefundRequest extends AppCompatActivity implements View.OnClickListener {
    ImageView backIv;
 SharedPref sharedPref;
    ModelUserData modelUserData;
    Context mContext;
    CustomTextSemiBold heading;
    CustomTextSemiBold tVClickHereLink;
    CustomTextSemiBold tvOkRefund;
    CustomTextSemiBold tvCancel;
    ModelOrderListData.Data dataItem;
    CustomTextMedium orderId;
    CustomTextMedium orderAmount;
    LinearLayout addAccountDetail;
    String withdrawalStatus="1";
CustomEditText accountNumber;
CustomEditText tvIfsc;
CustomEditText tvHoldersName;
CustomEditText remark;
CustomEditText mobileTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_request);
        mContext=ActivityRefundRequest.this;
        sharedPref = SharedPref.getInstance(mContext);
        modelUserData = sharedPref.getUser(Consts.Status.SP);

        initial();
    }
    void initial(){
backIv=findViewById(R.id.backIv);


        accountNumber=findViewById(R.id.accountNumber);
        tVClickHereLink=findViewById(R.id.TVClickHereLink);
        tVClickHereLink.setOnClickListener(this);
orderId=findViewById(R.id.orderId);
        tvIfsc=findViewById(R.id.tvIFSC);
        orderAmount=findViewById(R.id.orderAmount);
        tvOkRefund=findViewById(R.id.tvOkRefund);
        tvCancel=findViewById(R.id.tvCancel);
        remark=findViewById(R.id.remark);
        mobileTv=findViewById(R.id.mobileTv);
        mobileTv.setText(""+modelUserData.getData().getUserMobile());
        tvHoldersName=findViewById(R.id.tvHoldersName);
        tvCancel.setOnClickListener(this);
        tvOkRefund.setOnClickListener(this);
        addAccountDetail=findViewById(R.id.addAccountDeatil);
heading=findViewById(R.id.Heading);
heading.setText(R.string.refunt_adjustment);
backIv.setOnClickListener(this);
        if(getIntent().hasExtra("data")){
            dataItem=(ModelOrderListData.Data) getIntent().getSerializableExtra("data");
        orderId.setText("Order Number "+dataItem.getId());
            orderAmount.setText("Amount  ₹"+dataItem.getTotalAmount() +"\nOn Date "+dataItem.getSaleDate());
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.backIv:
            case R.id.tvCancel:
                onBackPressed();
                break;
            case R.id.TVClickHereLink:
                if(tVClickHereLink.getText().toString().equalsIgnoreCase("Click here to add account details.")){
                addAccountDetail.setVisibility(View.VISIBLE);
                tVClickHereLink.setText(R.string.click_here_to_hide);
                }else{
                    addAccountDetail.setVisibility(View.GONE);
                    tVClickHereLink.setText(R.string.click_here_to_add_account);
                }
                break;

            case R.id.tvOkRefund:
                if(!accountNumber.getText().toString().isEmpty()){
                    withdrawalStatus="2";
                    if(tvIfsc.getText().toString().isEmpty()){
                        tvIfsc.setError("IFSC required!");
                        tvIfsc.requestFocus();
                    }else{
                        if(tvHoldersName.getText().toString().isEmpty()){
                            tvHoldersName.requestFocus();
                            tvHoldersName.setError("Required field!");
                        }else{

                    apiRefund();}}

                }else{
                    withdrawalStatus="1";
                    apiRefund();
                }

                break;
        }
    }
    void apiRefund(){


ProjectUtils.showProgressDialog(mContext, false, "Loading...");
        AndroidNetworking.post(Consts.Status.BASE_URL +Consts.Status.API_REFUND_NOTICE)
                .addBodyParameter("user_id",""+modelUserData.getData().getUserId())
                .addBodyParameter("order_id",""+dataItem.getId())
                .addBodyParameter("withdrawal_status",""+withdrawalStatus)
                .addBodyParameter("account_number",""+accountNumber.getText().toString())
                .addBodyParameter("ifsc_code",""+tvIfsc.getText().toString())
                .addBodyParameter("holder_name",""+tvHoldersName.getText().toString())
                .addBodyParameter("user_mobile",""+mobileTv.getText().toString())
                .addBodyParameter("comments",""+remark.getText().toString())

                .setPriority(Priority.IMMEDIATE)

                .build()
                .getAsString(new StringRequestListener() {
                    @Override
                    public void onResponse(String response) {
                        ProjectUtils.pauseProgressDialog();

                        try {
                            JSONObject jsonObject=new JSONObject(response);
                            if(jsonObject.getString("status").equalsIgnoreCase("0")){
                                Toast.makeText(mContext,""+jsonObject.getString("msg"),Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(mContext,"Request for refund sent successfully..",Toast.LENGTH_SHORT).show();
                                tvOkRefund.setVisibility(View.GONE);
                                tvCancel.setVisibility(View.GONE);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(ANError anError) {
                        ProjectUtils.pauseProgressDialog();
                    }
                });
    }
}
