/*
 *
 *  * Copyright 2014 http://Bither.net
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *    http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package net.bither.ui.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

import net.bither.R;
import net.bither.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songchenwen on 15/2/13.
 */
public class DialogFragmentHDMSingularColdSeed extends DialogFragment implements View
        .OnClickListener, ViewPager.OnPageChangeListener {
    public static interface DialogFragmentHDMSingularColdSeedListener {
        public void HDMSingularColdSeedRemembered();
    }

    public static final String FragmentTag = "DialogFragmentHDMSingularColdSeed";
    public static final String WordsKey = "Content";
    public static final String LabelKey = "Label";
    public static final String ButtonKey = "Button";

    private List<String> words;
    private ViewPager pager;
    private PagerAdapter adapter;
    private DialogFragmentHDMSingularColdSeedListener listener;
    private Activity activity;

    private int labelStr;
    private int buttonStr;

    public static DialogFragmentHDMSingularColdSeed newInstance(List<String> words,
                                                                DialogFragmentHDMSingularColdSeedListener listener) {
        DialogFragmentHDMSingularColdSeed dialog = new DialogFragmentHDMSingularColdSeed();
        Bundle bundle = new Bundle();
        bundle.putSerializable(WordsKey, new ArrayList<String>(words));
        dialog.setArguments(bundle);
        dialog.listener = listener;
        return dialog;
    }

    public static DialogFragmentHDMSingularColdSeed newInstance(List<String> words,
                                                                int label, int button,
                                                                DialogFragmentHDMSingularColdSeedListener listener) {
        DialogFragmentHDMSingularColdSeed dialog = new DialogFragmentHDMSingularColdSeed();
        Bundle bundle = new Bundle();
        bundle.putSerializable(WordsKey, new ArrayList<String>(words));
        bundle.putInt(LabelKey, label);
        bundle.putInt(ButtonKey, button);
        dialog.setArguments(bundle);
        dialog.listener = listener;
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.QrCodePager);
        words = (List<String>) getArguments().getSerializable(WordsKey);
        labelStr = getArguments().getInt(LabelKey, 0);
        buttonStr = getArguments().getInt(ButtonKey, 0);
        adapter = new PagerAdapter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View vContainer = inflater.inflate(R.layout.dialog_hdm_singular_cold_seed, container,
                false);
        pager = (ViewPager) vContainer.findViewById(R.id.pager);
        vContainer.findViewById(R.id.btn_confirm).setOnClickListener(this);
        pager.setOffscreenPageLimit(1);
        int size = Math.min(UIUtil.getScreenWidth(), UIUtil.getScreenHeight());
        pager.getLayoutParams().width = pager.getLayoutParams().height = size;
        pager.setAdapter(adapter);
        pager.setCurrentItem(0);
        pager.setOnPageChangeListener(this);
        if (labelStr != 0) {
            TextView tvLabel = (TextView) vContainer.findViewById(R.id.tv_label);
            tvLabel.setText(labelStr);
        }
        if (buttonStr != 0) {
            Button tvLabel = (Button) vContainer.findViewById(R.id.btn_confirm);
            tvLabel.setText(buttonStr);
        }
        return vContainer;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        setCancelable(false);
        return dialog;
    }

    @Override
    public void onStart() {
        activity = getActivity();
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);
        super.onStart();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (listener != null) {
            listener.HDMSingularColdSeedRemembered();
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    private class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter() {
            super(getChildFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            return DialogFragmentHDMSingularColdSeedWords.newInstance(words);
        }

        @Override
        public int getCount() {
            return 1;
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
