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

package com.ntels.kneet.v3.view.dialog;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActivity;
import com.ntels.kneet.v3.common.widget.CustomScrollView;
import com.ntels.kneet.v3.R;

import java.util.ArrayList;

public class Dialog_Spinner extends CustomActivity implements View.OnClickListener {

    /** Called when the activity is first created. */

    private Activity act;
    Intent intent;
    Button btnCheck[];
    private AppConfig appConfig;
    private Util util = new Util();
    ArrayList<String> aTitleList    = new ArrayList<String>();
    ArrayList<String> aTitleSubList = new ArrayList<String>();
    ArrayList<String> aIconList     = new ArrayList<String>();
    ArrayList<String> aListResult   = new ArrayList<String>();
    private String sTitle     = "";
    private String sIconTitle = "";
    private String sBtn1      = "";
    private String sBtn2      = "";
    private int    iPosition  = -1;
    private int    iType      = 0;

    /**
     * <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 = Dialog_Spinner.this;
        appConfig = (AppConfig) act.getApplicationContext();

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

        if ( savedInstanceState != null ) {
            Log.i("Dialog_Spinner", "savedInstanceState is not null");
            sTitle = savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE).substring(4);
            sIconTitle = savedInstanceState.getString(Constants.EXTRA_TITLE_ICON);
            aTitleList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            aTitleSubList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            aIconList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST);
            aListResult = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택, 2: 세로 여러개,
            iType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE, 0);
            sBtn1 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            sBtn2 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            iPosition = savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, -1);
        } else {
            Intent intent = getIntent();
            sTitle = intent.getStringExtra(Constants.EXTRA_SPINNER_TITLE);
            sIconTitle = intent.getStringExtra(Constants.EXTRA_TITLE_ICON);
            aTitleList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_LIST1);
            aTitleSubList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_LIST2);
            aIconList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_ICON_LIST);
            aListResult = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택, 2: 세로 여러개,
            iType = intent.getIntExtra(Constants.EXTRA_SPINNER_TYPE, 0);
            sBtn1 = intent.getStringExtra(Constants.EXTRA_SPINNER_BTN1);
            sBtn2 = intent.getStringExtra(Constants.EXTRA_SPINNER_BTN2);
            iPosition = intent.getIntExtra(Constants.EXTRA_SPINNER_POSITION, -1);
        }
        if ( sTitle == null ) {
            sTitle = util.getString(act, R.string.layout_selected);
        }
        if ( sIconTitle == null ) {
            util.getView(act, R.id.ivTitleIcon).setVisibility(View.GONE);
        } else {
            util.getView(act, R.id.ivTitleIcon).setVisibility(View.VISIBLE);
            util.setImage(act, sIconTitle, util.getImageView(act, R.id.ivTitleIcon));
        }
        if ( sBtn1 == null ) {
            sBtn1 = util.getString(act, R.string.layout_selected);
        }
        if ( sBtn2 == null ) {
            sBtn2 = util.getString(act, R.string.layout_cancel);
        }
        util.getTextView(act, R.id.tvTitle).setText(sTitle);
        util.getTextView(act, R.id.tvBtn1).setText(sBtn1);
        util.getTextView(act, R.id.tvBtn2).setText(sBtn2);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.llBtn2).setOnClickListener(this);
        CustomScrollView svBody = (CustomScrollView) findViewById(R.id.svBody);
        //		svBody.setOnScrollViewChange(new OnScrollViewListener() {
        //			@Override
        //			public void onScrollChanged(int l, int t, int oldl, int oldt) {
        //				if (t > 0) {
        //					util.getImageView(act, R.id.ivShadow).setVisibility(View.VISIBLE);
        //				} else {
        //					util.getImageView(act, R.id.ivShadow).setVisibility(View.GONE);
        //				}
        //			}
        //
        //			@Override
        //			public void onScrollBottomDetect() {
        //				// Dlog.d("androidcasts", "onScrollBottomDetect");
        //			}
        //
        //			@Override
        //			public void onScrollTopDetect() {
        //				// Dlog.d("androidcasts", "onScrollTopDetect");
        //			}
        //		});
        setList();
    }

    @Override protected void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);
        if ( savedInstanceState != null ) {
            sTitle = savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE);
            sIconTitle = savedInstanceState.getString(Constants.EXTRA_TITLE_ICON);
            aTitleList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            aTitleSubList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            aIconList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST);
            aListResult = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택
            iType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE, 0);
            sBtn1 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            sBtn2 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            iPosition = savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, -1);
        }
    }

    @Override protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        outState.putString(Constants.EXTRA_SPINNER_TITLE, sTitle);
        outState.putString(Constants.EXTRA_TITLE_ICON, sIconTitle);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_LIST1, aTitleList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_LIST2, aTitleSubList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST, aIconList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST, aListResult);
        // 0: 세로 한개 선택, 1: 가로 한개선택
        outState.putInt(Constants.EXTRA_SPINNER_TYPE, iType);
        outState.putString(Constants.EXTRA_SPINNER_BTN1, sBtn1);
        outState.putString(Constants.EXTRA_SPINNER_BTN2, sBtn2);
        outState.putInt(Constants.EXTRA_SPINNER_POSITION, iPosition);
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    /******************************************************************************************************************
     * setList : dialog에 childView 붙이는 작업.
     *****************************************************************************************************************/
    private void setList() {

        LinearLayout llChildView = null, llBody = null;
        if ( iType == 0 ) {
            ((LinearLayout) findViewById(R.id.llBody1)).setVisibility(View.VISIBLE);
            ((LinearLayout) findViewById(R.id.llBody2)).setVisibility(View.GONE);
            ((LinearLayout) findViewById(R.id.llBodyList)).removeAllViews();
            llBody = util.getLinearLayout(act, R.id.llBodyList);
        } else if ( iType == 1 ) {
            ((LinearLayout) findViewById(R.id.llBody1)).setVisibility(View.GONE);
            ((LinearLayout) findViewById(R.id.llBody2)).setVisibility(View.VISIBLE);
            ((LinearLayout) findViewById(R.id.llBody2)).removeAllViews();
            llBody = util.getLinearLayout(act, R.id.llBody2);
        } else if ( iType == 2 ) {
            ((LinearLayout) findViewById(R.id.llBody1)).setVisibility(View.VISIBLE);
            ((LinearLayout) findViewById(R.id.llBody2)).setVisibility(View.GONE);
            ((LinearLayout) findViewById(R.id.llBodyList)).removeAllViews();
            llBody = util.getLinearLayout(act, R.id.llBodyList);
        }
        if ( aTitleList.size() > 0 ) {
            btnCheck = new Button[aTitleList.size()];
            for ( int iRow = 0; iRow < aTitleList.size(); iRow++ ) {
                if ( iType == 0 ) {
                    llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_row1, null);
                    ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(aTitleList.get(iRow));
                } else if ( iType == 1 ) {
                    llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_row2, null);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                    params.weight = 1.0f;
                    llChildView.setLayoutParams(params);
                } else if ( iType == 2 ) {
                    llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_row3, null);
                }
                llBody.addView(llChildView);
                btnCheck[iRow] = (Button) llChildView.findViewById(R.id.btnCheck);
                if ( iType == 1 ) {
                    ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(aTitleList.get(iRow));
                } else {
                    if ( aTitleSubList == null ) {
                        ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(aTitleList.get(iRow));
                        ((TextView) llChildView.findViewById(R.id.tvBody2)).setVisibility(View.GONE);
                    } else {
                        ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(aTitleList.get(iRow));
                        ((TextView) llChildView.findViewById(R.id.tvBody2)).setVisibility(View.VISIBLE);
                        ((TextView) llChildView.findViewById(R.id.tvBody2)).setText(aTitleSubList.get(iRow));
                    }
                    if ( aIconList == null ) {
                        ((ImageView) llChildView.findViewById(R.id.ivIcon)).setVisibility(View.GONE);
                    } else {
                        ((ImageView) llChildView.findViewById(R.id.ivIcon)).setVisibility(View.VISIBLE);
                        util.setImage(act, aIconList.get(iRow), ((ImageView) llChildView.findViewById(R.id.ivIcon)));
                    }
                }
                btnCheck[iRow].setTag(iRow);
                btnCheck[iRow].setOnClickListener(this);
                // llChildView.setOnClickListener(this);
                llChildView.setTag(iRow);
                llChildView.setOnClickListener(this);
                if ( iType != 2 ) {
                    if ( iRow == iPosition ) {
                        btnCheck[iRow].setSelected(true);
                    }
                }
            }
        }
        if ( iType == 2 ) {
            for ( int iRow = 0; iRow < aListResult.size(); iRow++ ) {
                btnCheck[util.str2int(aListResult.get(iRow))].setSelected(true);
            }
        }
    }

    /********************************************************************************************************
     * onClick이벤트
     *
     * @param
     *
     * @return
     ********************************************************************************************************/
    @Override public void onClick(View v) {

        switch (v.getId()) {
            case R.id.llBtn1:
                intent = new Intent();
                if ( iType == 2 ) {
                    aListResult = new ArrayList<String>();
                    for ( int iRow = 0; iRow < aTitleList.size(); iRow++ ) {
                        if ( btnCheck[iRow].isSelected() ) {
                            aListResult.add("" + iRow);
                        }
                    }
                    intent.putExtra(Constants.EXTRA_SPINNER_RESULT_LIST, aListResult);
                } else {
                    intent.putExtra(Constants.EXTRA_SPINNER_RESULT, iPosition);
                }
                act.setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.llBody:
            case R.id.btnCheck:
                int iChekRow = (Integer) v.getTag();
                if ( iType == 2 ) {
                    if ( v.isSelected() ) {
                        v.setSelected(false);
                    } else {
                        v.setSelected(true);
                    }
                } else {
                    for ( int iRow = 0; iRow < aTitleList.size(); iRow++ ) {
                        if ( btnCheck[iRow].isSelected() ) {
                            btnCheck[iRow].setSelected(false);
                        }
                    }
                    btnCheck[iChekRow].setSelected(true);
                    iPosition = iChekRow;
                }
                break;
            default:
                break;
        }
    }

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

        super.onResume();

        // if (!GreenBilllab.getIntro()) {
        // if (Main.reLoginCheck()) {
        // onBackPressed();
        // }
        // }
    }

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

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