/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.view;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.c_interface.OnDataSendToReslut;
import com.ntels.kneet.v3.common.http.HttpSend;
import com.ntels.kneet.v3.common.http.Response;
import com.ntels.kneet.v3.common.util.Dlog;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActionBarActivity;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

import org.json.JSONObject;

public class Reset_Password extends CustomActionBarActivity implements View.OnClickListener,
                                                                       CustomDialog.OnMsgAlterToResult {

    private Toolbar  toolbar;
    private Activity act;
    Intent intent;
    private AppConfig appConfig;
    private              Util         util                      = new Util();
    private              CustomDialog customDialog              = new CustomDialog();
    private              int          iType                     = 0;
    private              String       sEMAIL_ID                 = "";
    private static final int          MSG_COM_P_MEMBER_PASSWORD = 1;

    /**
     * <PRE>
     * 1. MethodName: onCreate
     * 2. ClassName :
     * 3. Comment   : onCreate
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        act = Reset_Password.this;
        appConfig = (AppConfig) act.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

        View viewToLoad = LayoutInflater.from(act).inflate(R.layout.reset_password, null);
        this.setContentView(viewToLoad);

        setToolbar();

        displayListView();
    }

    private void setToolbar() {

        toolbar = (Toolbar) act.findViewById(R.id.toolbar);
        util.setKeyboardHide(act, toolbar);
        util.getTextView(toolbar, R.id.tvToolbarTitle1).setVisibility(View.VISIBLE);
        util.getTextView(toolbar, R.id.tvToolbarTitle2).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitleDivider).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle3).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle4).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarDel).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);

        util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_reset_password_01));
    }

    private void displayListView() {

        util.getView(act, R.id.btnSendEmail).setOnClickListener(this);
        util.getView(act, R.id.btnReceiverEmail).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnSendEmail:
                Uri uri = Uri.parse("mailto:help@allofthings.com");
                Intent it = new Intent(Intent.ACTION_SENDTO, uri);
                startActivity(it);
                break;
            case R.id.btnReceiverEmail:
                if ( !checkFormValid() ) {
                    return;
                }
                v.setEnabled(false);
                ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_P_MEMBER_PASSWORD), 10);
                break;
            case R.id.llBtn1:
                onBackPressed();
                break;
            default:
                break;
        }
    }

    private Boolean checkFormValid() {

        Boolean bCheck = true;
        sEMAIL_ID = util.getEditTextVal(act, R.id.edtEmail);
        if ( sEMAIL_ID.trim().length() < 1 ) {
            bCheck = false;
            util.setShowToast(act, util.getString(act, R.string.layout_join_03) + util.getString(act, R.string.MSG902), Toast.LENGTH_SHORT);
            return bCheck;
        }

        return bCheck;
    }

    /**
     * <PRE>
     * 1. MethodName: onBackPressed
     * 2. ClassName :
     * 3. Comment   : onBackPressed
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onBackPressed() {

        intent = new Intent();
        act.setResult(RESULT_CANCELED, intent);
        finish();
        return;
    }

    /**
     * <PRE>
     * 1. MethodName: onResume
     * 2. ClassName :
     * 3. Comment   : onResume
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onResume() {

        super.onResume();
    }

    Handler ComHandler = new Handler(new IncomingHandlerCallBack());

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override public boolean handleMessage(Message msg) {

            iType = msg.what;
            switch (msg.what) {
                case MSG_COM_P_MEMBER_PASSWORD:
                    send_P_member_password();
                    break;
                default:
                    break;
            }
            return true;
        }
    }

    private void send_P_member_password() {

        try {
            HttpSend httpSend = new HttpSend(act);
            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override public void resultResponse(Response result) {

                    result_P_member_password(result);
                }
            });
            httpSend.P_member_password(sEMAIL_ID, appConfig.getService_Id());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void result_P_member_password(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {

                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                customDialog.MsgAlert(act, Constants.MSG840);
            } else {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                String sERR_CODE = util.null2string(joResult.get(Constants.HTTP_err_code));
                String sERR_MSG = util.null2string(joResult.get(Constants.HTTP_err_msg));

                customDialog.MsgAlert(act, Constants.MSG000, sERR_MSG);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }

    @Override public void resultCustomDialogResponse(int iMsgID, int iBtnResult) {

        switch (iMsgID) {

            case Constants.MSG807:
                if ( iBtnResult == 1 ) {
                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(iType), 10);
                } else {
                    act.finish();
                }
                break;
            case Constants.MSG840:
                act.finish();
                break;
            default:
                break;
        }
    }
}
