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

package com.ntels.onecable.admin.view.dialog;

import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.util.Dlog;
import com.ntels.onecable.admin.common.util.Util;
import com.ntels.onecable.admin.common.widget.CustomActivity;

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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * <pre>
 * 1. ClassName :   DialogSpinner
 * 2. FileName  :   DialogSpinner.java
 * 3. Package   :   com.ntels.onecable.admin.view.dialog
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class DialogSpinner extends CustomActivity implements View.OnClickListener {

    private static final String TAG = DialogSpinner.class.getSimpleName();
    private Intent mIntent;
    private LinearLayout[] mCheck;
    private ArrayList<String> mTitleList = new ArrayList<>();
    private ArrayList<String> mTitleSubList = new ArrayList<>();
    private ArrayList<String> mTitleEnableList = new ArrayList<>();
    private ArrayList<String> mIconList = new ArrayList<>();
    private ArrayList<String> mListResult = new ArrayList<>();
    private Activity mActivity;
    private Util mUtil = new Util();
    private String mTitle = "";
    private String mBtn1 = "";
    private String mBtn2 = "";
    private int mPosition = -1;
    private int mType = 0;
    private int mShowType = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = DialogSpinner.this;

        if (savedInstanceState != null) {
            Log.i("DialogSpinner", "savedInstanceState is not null");
            mTitle = savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE).substring(4);
            mTitleList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            mTitleSubList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            mTitleEnableList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST3);
            mIconList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST);
            mListResult = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택, 2: 세로 여러개,
            mType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE, 0);
            mShowType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_SHOW_TYPE, 0);
            mBtn1 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            mBtn2 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            mPosition = savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, -1);
        } else {
            Intent intent = getIntent();
            mTitle = intent.getStringExtra(Constants.EXTRA_SPINNER_TITLE);
            mTitleList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_LIST1);
            mTitleSubList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_LIST2);
            mTitleEnableList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_LIST3);
            mIconList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_ICON_LIST);
            mListResult = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택, 2: 세로 여러개,
            mType = intent.getIntExtra(Constants.EXTRA_SPINNER_TYPE, 0);
            // 0 : 팝업 , 1: 아래서 위로
            mShowType = intent.getIntExtra(Constants.EXTRA_SPINNER_SHOW_TYPE, 0);
            mBtn1 = intent.getStringExtra(Constants.EXTRA_SPINNER_BTN1);
            mBtn2 = intent.getStringExtra(Constants.EXTRA_SPINNER_BTN2);
            mPosition = intent.getIntExtra(Constants.EXTRA_SPINNER_POSITION, -1);
        }
        if (mTitle == null) {
            mTitle = mUtil.getString(mActivity, R.string.layout_selected);
        }

        Dlog.v("jino1111");
        View viewToLoad = LayoutInflater.from(mActivity).inflate(R.layout.dialog_spinner, null);
        this.setContentView(viewToLoad);
        if (mShowType == 0) {
            mUtil.getView(mActivity, R.id.llMain1).setVisibility(View.VISIBLE);
            mUtil.getView(mActivity, R.id.llMain2).setVisibility(View.GONE);

            mUtil.getView(mActivity, R.id.ivTitleIcon).setVisibility(View.GONE);

            if (mBtn1 == null) {
                mBtn1 = mUtil.getString(mActivity, R.string.layout_ok);
            }
            if (mBtn2 == null) {
                mBtn2 = mUtil.getString(mActivity, R.string.layout_cancel);
            }
            mUtil.getTextView(mActivity, R.id.tvTitle).setText(mTitle);
            mUtil.getTextView(mActivity, R.id.tvBtn1).setText(mBtn1);
            mUtil.getTextView(mActivity, R.id.tvBtn2).setText(mBtn2);
        } else {
            Dlog.v("jino1112");
            mUtil.getView(mActivity, R.id.llMain1).setVisibility(View.GONE);
            mUtil.getView(mActivity, R.id.llMain2).setVisibility(View.VISIBLE);
            mUtil.getTextView(mActivity, R.id.tvTitle2).setText(mTitle);
        }
        mUtil.getView(mActivity, R.id.llBtn1).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.llBtn2).setOnClickListener(this);
        mUtil.getView(mActivity, R.id.btnClose2).setOnClickListener(this);

        setList();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            mTitle = savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE);
            mTitleList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            mTitleSubList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            mTitleEnableList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST3);
            mIconList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST);
            mListResult = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택
            mType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE, 0);
            mShowType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_SHOW_TYPE, 0);
            mBtn1 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            mBtn2 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            mPosition = savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, -1);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        outState.putString(Constants.EXTRA_SPINNER_TITLE, mTitle);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_LIST1, mTitleList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_LIST2, mTitleSubList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_LIST3,  mTitleEnableList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST, mIconList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST, mListResult);
        // 0: 세로 한개 선택, 1: 가로 한개선택
        outState.putInt(Constants.EXTRA_SPINNER_TYPE, mType);
        outState.putInt(Constants.EXTRA_SPINNER_SHOW_TYPE, mShowType);
        outState.putString(Constants.EXTRA_SPINNER_BTN1, mBtn1);
        outState.putString(Constants.EXTRA_SPINNER_BTN2, mBtn2);
        outState.putInt(Constants.EXTRA_SPINNER_POSITION, mPosition);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

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

        LinearLayout llChildView = null;
        LinearLayout llBody = null;

        Dlog.v("jino1113");
        if (mShowType == 0) {
            if (mType == 0) {
                findViewById(R.id.llBody1).setVisibility(View.VISIBLE);
                findViewById(R.id.llBody2).setVisibility(View.GONE);
                ((LinearLayout) findViewById(R.id.llBodyList)).removeAllViews();
                llBody = mUtil.getLinearLayout(mActivity, R.id.llBodyList);
            } else if (mType == 1) {
                findViewById(R.id.llBody1).setVisibility(View.GONE);
                findViewById(R.id.llBody2).setVisibility(View.VISIBLE);
                ((LinearLayout) findViewById(R.id.llBody2)).removeAllViews();
                llBody = mUtil.getLinearLayout(mActivity, R.id.llBody2);
            } else if (mType == 2) {
                findViewById(R.id.llBody1).setVisibility(View.VISIBLE);
                findViewById(R.id.llBody2).setVisibility(View.GONE);
                ((LinearLayout) findViewById(R.id.llBodyList)).removeAllViews();
                llBody = mUtil.getLinearLayout(mActivity, R.id.llBodyList);
            }
            if (mTitleList.size() > 0) {
                mCheck = new LinearLayout[mTitleList.size()];
                for (int row = 0; row < mTitleList.size(); row++) {
                    if (mType == 0) {
                        llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_main1_row1, null);
                        ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(mTitleList.get(row));
                    } else if (mType == 1) {
                        llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_main1_row2, null);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams
                                .MATCH_PARENT);
                        params.weight = 1.0f;
                        llChildView.setLayoutParams(params);
                    } else if (mType == 2) {
                        llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_main1_row3, null);
                    }
                    llBody.addView(llChildView);
                    mCheck[row] = (LinearLayout) llChildView.findViewById(R.id.llBodyCheck);
                    if (mType == 1) {
                        ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(mTitleList.get(row));
                    } else {
                        if (mTitleSubList == null) {
                            ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(mTitleList.get(row));
                            llChildView.findViewById(R.id.tvBody2).setVisibility(View.GONE);
                        } else {
                            ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(mTitleList.get(row));
                            llChildView.findViewById(R.id.tvBody2).setVisibility(View.VISIBLE);
                            ((TextView) llChildView.findViewById(R.id.tvBody2)).setText(mTitleSubList.get(row));
                        }
                        Dlog.v("jino1114");
                        if (mTitleEnableList == null) {
                            Dlog.v("jino1115");
                            mCheck[row].setEnabled(true);
                            llChildView.setEnabled(true);
                        } else {
                            if (mTitleEnableList.get(row).equals("Y")){
                                Dlog.v("jino1116");
                                mCheck[row].setEnabled(true);
                                llChildView.setEnabled(true);
                            } else {
                                Dlog.v("jino1117");
                                mCheck[row].setEnabled(false);
                                llChildView.setEnabled(false);
                            }
                        }
                        if (mIconList == null) {
                            llChildView.findViewById(R.id.ivIcon).setVisibility(View.GONE);
                        } else {
                            llChildView.findViewById(R.id.ivIcon).setVisibility(View.VISIBLE);
                            mUtil.setImage(mActivity, mIconList.get(row), ((ImageView) llChildView.findViewById(R.id.ivIcon)));
                        }
                    }
                    mCheck[row].setTag(row);
                    mCheck[row].setOnClickListener(this);
                    llChildView.setTag(row);
                    llChildView.setOnClickListener(this);
                    if (mType != 2) {
                        if (row == mPosition) {
                            mCheck[row].setSelected(true);
                        }
                    }
                }
            }
            if (mType == 2) {
                for (int row = 0; row < mListResult.size(); row++) {
                    mCheck[mUtil.str2int(mListResult.get(row))].setSelected(true);
                }
            }
        } else {
            Dlog.v("jino1114");
            ((LinearLayout) findViewById(R.id.llBodyList2)).removeAllViews();
            llBody = mUtil.getLinearLayout(mActivity, R.id.llBodyList2);
            if (mTitleList.size() > 0) {
                Dlog.v("jino11115");
                mCheck = new LinearLayout[mTitleList.size()];
                for (int row = 0; row < mTitleList.size(); row++) {
                    Dlog.v("jino11116");
                    llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_main2_row1, null);
                    ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(mTitleList.get(row));
                    llBody.addView(llChildView);
                    mCheck[row] = (LinearLayout) llChildView.findViewById(R.id.llBodyCheck);
                    mCheck[row].setTag(row);
                    mCheck[row].setOnClickListener(this);
                    llChildView.setTag(row);
                    llChildView.setOnClickListener(this);
                    if (mTitleEnableList == null) {
                        llChildView.setEnabled(true);
                    } else {
                        if (mTitleEnableList.get(row).equals("Y")){
                            llChildView.setEnabled(true);
                        } else {
                            llChildView.setEnabled(false);
                        }
                    }
                    if (row == mPosition) {
                        mCheck[row].setSelected(true);
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                Dlog.v("111111111");
                mIntent = new Intent();
                if (mType == 2) {
                    mListResult = new ArrayList<>();
                    for (int row = 0; row < mTitleList.size(); row++) {
                        if (mCheck[row].isSelected()) {
                            mListResult.add("" + row);
                        }
                    }
                    mIntent.putExtra(Constants.EXTRA_SPINNER_RESULT_LIST, mListResult);
                } else {
                    mIntent.putExtra(Constants.EXTRA_SPINNER_RESULT, mPosition);
                }
                mActivity.setResult(RESULT_OK, mIntent);
                finish();
                break;
            case R.id.llBtn2:
            case R.id.btnClose2:
                onBackPressed();
                break;
            case R.id.llBody:
            case R.id.llBodyCheck:
                int checkRow = (Integer) view.getTag();
                if (mType == 2) {
                    if (view.isSelected()) {
                        view.setSelected(false);
                    } else {
                        view.setSelected(true);
                    }
                } else {
                    for (int row = 0; row < mTitleList.size(); row++) {
                        if (mCheck[row].isSelected()) {
                            mCheck[row].setSelected(false);
                        }
                    }
                    mCheck[checkRow].setSelected(true);
                    mPosition = checkRow;
                }
                Dlog.v("11111");
                if (mShowType != 0) {
                    Dlog.v("11111111");
                    mUtil.getView(mActivity, R.id.llBtn1).performClick();
//                    onClick(mUtil.getView(mActivity, R.id.llBtn1));
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

        mIntent = new Intent();
        mActivity.setResult(RESULT_CANCELED, mIntent);
        finish();
    }
}
