package com.sendsms.activity;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sendsms.Parsing.MessageParsing;
import com.sendsms.R;
import com.sendsms.fragment.FragmentDetails;
import com.sendsms.model.UserDetails;
import com.sendsms.utills.Util;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tv_submit;
    private EditText iv_userName,iv_userPassword;

    private String str_iv_userName,str_iv_userPassword;

    public Context mContext;

    ArrayList<UserDetails> UserListArr = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initializeView();
    }

    private void initializeView()
    {

        this.mContext = this;
        iv_userName = (EditText) findViewById(R.id.iv_userName);
        iv_userPassword = (EditText) findViewById(R.id.iv_userPassword);
        tv_submit = (TextView) findViewById(R.id.tv_submit);

        tv_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_submit:
            {
                str_iv_userName = iv_userName.getText().toString().trim();
                str_iv_userPassword = iv_userPassword.getText().toString().trim();

                if (str_iv_userName.length() == 0 && str_iv_userPassword.length() == 0)
                {
                    Toast.makeText(getApplicationContext(),"Please fill all fields.",Toast.LENGTH_SHORT).show();
                }
                else if (str_iv_userName.length() == 0)
                {
                    Toast.makeText(getApplicationContext(),"Please Enter User Name",Toast.LENGTH_SHORT).show();
                }
                else if (str_iv_userPassword.length() == 0)
                {
                    Toast.makeText(getApplicationContext(),"Please Enter User Password",Toast.LENGTH_SHORT).show();
                }
                else {

                    if (Util.isNetworkAvailable(mContext)) {
                        new GetAllUserListTask().execute();
                    } else {
                        Util.customDialog(mContext, "No Network Connection ");
                    }
                }
                break;
            }
        }
    }



    private class GetAllUserListTask extends AsyncTask<Void, Void, Object[]> {

        @Override
        public void onPreExecute() {
            Util.showProDialog(mContext);
        }

        @Override
        protected Object[] doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return MessageParsing.getAllUserDetails();
        }

        @Override
        public void onPostExecute(Object[] result) {
            Util.dismissDialog();
            try {
                boolean status = (Boolean) result[0];
                if (result != null && status) {

                    UserListArr = (ArrayList<UserDetails>) result[1];

                    boolean flag = false;

                    for (int i = 0; i < UserListArr.size(); i++) {
                        if (UserListArr.get(i).getName().equalsIgnoreCase(str_iv_userName) && UserListArr.get(i).getPassword().equalsIgnoreCase(str_iv_userPassword))
                        {
                            flag = true;
                            String URL = "http://"+UserListArr.get(i).getApi_base_url()+"/"+UserListArr.get(i).getApi_link_path()+"/"+UserListArr.get(i).getApi_name();
                            String mCaption = UserListArr.get(i).getCaption();

                            Intent intent = new Intent(LoginActivity.this,SmsDetailsActivity.class);
                            intent.putExtra("url",URL);
                            intent.putExtra("caption",mCaption);
                            startActivity(intent);
                            finish();
                            break;
                        }
                        else
                        {
                            flag = false;

                        }

                    }

                    if (!flag)
                    {
                        Toast.makeText(getApplicationContext(),"Please Correct User Name Or Password",Toast.LENGTH_SHORT).show();
                    }


                } else {
                    Util.customDialog(mContext, "Result not found");

                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }
    }
}
