package com.xplora.xpsettings.Fragment;

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xplora.xpsettings.Observer.Listener;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Macros;
import com.xplora.xpsettings.Utils.ResUtils;
import com.xplora.xpsettings.Utils.ToolsUtils;

public class RingtoneNewFragment extends DialogFragment implements View.OnClickListener {
    private Context mContext = null;
    private ImageView mIconIamge = null;
    private TextView mTitleText = null;

    private TextView mContentText = null;
    private TextView mContentHintText = null;

    private Button mTestButton = null;
    private Button mNoButton = null;
    private Button mYesButton = null;
    private Button mExitButton = null;

    private LinearLayout mButtonLayout = null;
    private ImageView mDownloadingImage = null;

    private Animation mDownloadingAnimation = null;

    private int mViewStatus = 0; //0,1:未下载， 2：下载中， 3：失败， 4：完成  10:中间状态，不刷新界面
    private String mRingtoneNames = "";
    private int mReason = 0;
    private boolean isSkipAgree = false; //是否模拟点击yes，即直接同意下载

    public static RingtoneNewFragment newInstance(Context context) {
        RingtoneNewFragment fragment = new RingtoneNewFragment();
        fragment.mContext = context;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

        //不能右滑退出
        this.setCancelable(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_ringtone_new, container, false);
        onCreateViewEx(rootView);
        if (isSkipAgree) {
            isSkipAgree = false;
            //模拟点击yes，
            onClick(mYesButton);
        } else {
            refreshView();
        }
        return rootView;
    }

    private void onCreateViewEx(View rootView) {
        mIconIamge = (ImageView)rootView.findViewById(R.id.mIconIamge);
        mTitleText = (TextView)rootView.findViewById(R.id.mTitleText);
        mContentText = (TextView)rootView.findViewById(R.id.mContentText);
        mContentHintText = (TextView)rootView.findViewById(R.id.mContentHintText);
        mDownloadingImage = (ImageView) rootView.findViewById(R.id.mDownloadingImage);

        mButtonLayout = (LinearLayout)rootView.findViewById(R.id.mButtonLayout);
        mNoButton = (Button)rootView.findViewById(R.id.mNoBtn);
        mYesButton = (Button)rootView.findViewById(R.id.mYesBtn);
        mExitButton = (Button)rootView.findViewById(R.id.mExitBtn);
        mTestButton = (Button)rootView.findViewById(R.id.mTestButton);

        mNoButton.setOnClickListener(this);
        mExitButton.setOnClickListener(this);
        mYesButton.setOnClickListener(this);
        mTestButton.setOnClickListener(this);
    }

    /*
    //0,1:未下载， 2：下载中， 3：失败， 4：完成  10:中间状态，不刷新界面
     */
    @SuppressLint("SetTextI18n")
    public void refreshView() {
        if (mViewStatus == 10)
            return;
        //icon
        int iconId = ResUtils.getImageId("ringtone_ic_", mViewStatus);
        mIconIamge.setImageResource(iconId);

        //title
        int titleId = mViewStatus == 0 ? R.string.ringtone_title_new : R.string.ringtone_title_download;
        mTitleText.setText(titleId);

        //Content
        String content = mViewStatus == 3 ? "" : mRingtoneNames;
        mContentText.setText(content);
        mContentText.setVisibility(mViewStatus == 3 ? View.GONE : View.VISIBLE);

        //ContentHint
        String contentHint = "";
        if (mViewStatus == 3) {
            mReason = Math.max(mReason, 0);
            int resId = ResUtils.getStringId("content_reason_", mReason);
            if (resId <= 0)
                resId = R.string.content_reason_0;
            contentHint = getString(resId);
        } else {
            contentHint = mViewStatus == 2 ? getString(R.string.content_downloading) : (mViewStatus == 0 ? getString(R.string.content_download) : getString(R.string.content_setup));
        }
        mContentHintText.setText(contentHint);

        //Downloading
        mDownloadingImage.setVisibility(mViewStatus == 2 ? View.VISIBLE : View.GONE);
        actionDownloading(mViewStatus == 2);

        //Button
        mButtonLayout.setVisibility(mViewStatus == 2 ? View.GONE : (mViewStatus == 4 ? View.INVISIBLE : View.VISIBLE));
        mNoButton.setVisibility(mViewStatus == 2 || (mViewStatus == 3 && mReason == 1) ? View.GONE : View.VISIBLE);
        mYesButton.setVisibility(mViewStatus == 2 || (mViewStatus == 3 && mReason == 1) ? View.GONE : View.VISIBLE);
        mExitButton.setVisibility(mViewStatus == 3 && (mReason == 1) ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View v) {
        if (v == mTestButton) {
            if (!Macros.DEBUG)
                return;
            switch (mViewStatus) {
                case 2:
                    Listener.ContentListener listener = (Listener.ContentListener)mContext;
                    listener.testRingtoneDBComplete();
                    break;
                case 3:
                    //下载中
                    break;
            }
        } else if (v == mNoButton || v == mExitButton) {
            Listener.ContentListener listener = (Listener.ContentListener)mContext;
            listener.onContentDownloadCancel(mViewStatus == 4);
            dismissAllowingStateLoss();
        } else if (v == mYesButton) {
            switch (mViewStatus) {
                case 0:
                    if (Macros.DEBUG_RINGTONG_NEW || ToolsUtils.isWifiConnected(mContext)) {
                        mViewStatus = 2;
                        refreshView();
                        Listener.ContentListener listener0 = (Listener.ContentListener) mContext;
                        listener0.onContentDownloadBegin();
                    } else {
                        mViewStatus = 3;
                        mReason = 100;
                        refreshView();
                    }
                    break;
                case 2:
                    //下载中
                    break;
                case 3:
                    if (mReason == 3 || mReason == 100) {
                        //连接wifi
                        Listener.ContentListener listener0 = (Listener.ContentListener) mContext;
                        listener0.onGotoWiFi();
                        dismissAllowingStateLoss();
                    } else {
                        //失败
                        mViewStatus = 2;
                        refreshView();
                        Listener.ContentListener listener0 = (Listener.ContentListener) mContext;
                        listener0.onContentDownloadBegin();
                    }
                    break;
                case 4:
                    //完成
                    Listener.ContentListener listener1 = (Listener.ContentListener) mContext;
                    listener1.onContentSetup();
                    dismissAllowingStateLoss();
                    break;
                default:
                    break;
            }
        }
    }

    public void setInitViewStatus(int status) {
        if (status == 2) {
            isSkipAgree = true;
        }
    }

    public void setItemInfo(String ringtoneNames) {
        mRingtoneNames = ringtoneNames;
    }

    public void changeViewViewStatus(int status, int reason) {
        mViewStatus = status;
        mReason = reason;
        refreshView();
    }

    public void actionDownloading(boolean isDownloading) {
        if (isDownloading) {
            if (mDownloadingAnimation == null) {
                mDownloadingAnimation = new RotateAnimation(0f, 360f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                mDownloadingAnimation.setDuration(1000);
                mDownloadingAnimation.setRepeatCount(-1);
                mDownloadingAnimation.setInterpolator(new LinearInterpolator());
            }
            mDownloadingImage.startAnimation(mDownloadingAnimation);
        } else {
            if (mDownloadingAnimation != null) {
                mDownloadingAnimation.cancel();
                mDownloadingAnimation = null;
            }
            if (mDownloadingImage != null) {
                mDownloadingImage.clearAnimation();
            }
        }
    }
}